EDIT #3: Lösung mit geänderter JSON-Struktur hier: Post #4
Lösung mit Überführung in Objekt hier: Post #7
Ich hatte hier auf meine JSON-Datei verwiesen.
Die Umsetzung in mein Projekt soll in Nim erfolgen.
Ich habe die Struktur der JSON-Datei mal heruntergebrochen auf ein Minimum:
{
"KEY": "Value-String", "# Version"
"KEY": "Value-String", "# Creator"
"KEY": [ "# Source: ARRAY OBJEKTE - keine fixe Anzahl"
{
"KEY": "Value-String", "# product-number"
"KEY": "Value-String" "# title"
}
],
"KEY": { "# error-list: OBJEKT"
"KEY": [ "# error-code: ARRAY OBJEKTE - keine fixe Anzahl"
{
"KEY": ["Array Strings - keine fixe Anzahl"], "# device"
"KEY": "Value-String", "# name"
"KEY": "Value-String", "# description"
"KEY": ["Array Strings - keine fixe Anzahl"], "# reason"
"KEY": ["Array Strings - keine fixe Anzahl"], "# disposal"
"KEY": "Value-String" "# remark"
}
]
}
}
Alles anzeigen
Die Nim JSON Library bietet die Möglichkeit, die geparsten JSON-Daten in ein Objekt zu überführen.
Somit habe ich auf obiger Struktur aufbauend meine Datentypen erstellt.
Hier mal meine Testdatei mit minimalem JSON-Musterdatensatz:
import json
type
oSource = object
product_number: string
product_title: string
oErrCode = object
device: openArray[string]
name: string
description: string
reason: openArray[string]
disposal: openArray[string]
remark: string
oErrListItem = object
code_number: string
code_item: openArray[oErrCode]
oErrList = object
errors: openArray[oErrListItem]
oError = object
version: string
craetor: string
source: openArray[oSource]
error_list: oErrList
let err_json =
"""
{
"version": "2022-09",
"creator": "BugFix",
"source": [
{
"product_number": "XYZ 123ab",
"product_title": "Servicemanual für Geräte des Typs A / B"
},
{
"product_number": "XYZ 123c",
"product_title": "Servicemanual für Geräte des Typs C"
},
{
"product_number": "XYZ 123def",
"product_title": "Servicemanual für Geräte des Typs D / E / F"
}
],
"error_list": {
"101": [
{
"device": ["dev A","dev B","dev C"],
"name": "SensorErrBla",
"description": "Sensorfehler",
"reason": ["Mainboard defekt"],
"disposal": ["Mainboard ersetzen"],
"remark": ""
},
{
"device": ["dev D","dev E","dev F"],
"name": "SensorErrBlub",
"description": "Sensorfehler",
"reason": ["Mainboard defekt"],
"disposal": ["Mainboard ersetzen"],
"remark": ""
}
],
"102": [
{
"device": ["dev A","dev B"],
"name": "BatteryErrBla",
"description": "Akkufehler",
"reason": ["Akku defekt","Controllerboard defekt"],
"disposal": ["Akku erstzen","Controllerboard ersetzen"],
"remark": "Bei Akkutausch: Reset Nutzung im Wartungsmenü"
},
{
"device": ["dev C"],
"name": "BatteryErrBlub",
"description": "Akkuladefehler",
"reason": ["Controllerboard defekt"],
"disposal": ["Controllerboard ersetzen"],
"remark": ""
},
{
"device": ["dev D","dev D],
"name": "BatteryErrBlaBlub",
"description": "Akkufehler",
"reason": ["Akku defekt","Akku nicht vorhanden"],
"disposal": ["Akku ersetzen","Akku einsetzen"],
"remark": "Bei Akkutausch: Reset Nutzung im Wartungsmenü"
}
]
}
}
"""
let
obj_json = parseJson(err_json) # JSON String parsen
oErrorsJSON = to(obj_json, oError) # JSON String in mein Objekt überführen
echo oErrorsJSON.creator
Alles anzeigen
Grundsätzlich ist die Überführung verschachtelteter Strukturen in ein Objekt möglich: Nim: How to parse nested JSON into objects
Im dortigen Bsp. sind jedoch alle Daten fix.
Ich habe jedoch in allen Arrays eine veränderliche Anzahl von Einträgen, deshalb die Nutzung von "openArray". Und hier liegt das Problem. Gleich beim ersten Auftauchen fliegt es mir um die Ohren:
Error: type mismatch: got <openArray[oSource], JsonNode, string>
but expected one of:
proc initFromJson(dst: var JsonNode; jsonNode: JsonNode; jsonPath: var string)
first type mismatch at position: 1
required type for dst: var JsonNode
but expression 'dst.source' is of type: openArray[oSource]
proc initFromJson(dst: var bool; jsonNode: JsonNode; jsonPath: var string)
first type mismatch at position: 1
required type for dst: var bool
but expression 'dst.source' is of type: openArray[oSource]
proc initFromJson(dst: var string; jsonNode: JsonNode; jsonPath: var string)
first type mismatch at position: 1
required type for dst: var string
but expression 'dst.source' is of type: openArray[oSource]
proc initFromJson[S, T](dst: var array[S, T]; jsonNode: JsonNode; jsonPath: var string)
first type mismatch at position: 1
required type for dst: var array[S, T]
but expression 'dst.source' is of type: openArray[oSource]
proc initFromJson[T: SomeFloat](dst: var T; jsonNode: JsonNode; jsonPath: var string)
first type mismatch at position: 1
required type for dst: var T: SomeFloat
but expression 'dst.source' is of type: openArray[oSource]
proc initFromJson[T: SomeInteger](dst: var T; jsonNode: JsonNode; jsonPath: var string)
first type mismatch at position: 1
required type for dst: var T: SomeInteger
but expression 'dst.source' is of type: openArray[oSource]
proc initFromJson[T: distinct](dst: var T; jsonNode: JsonNode; jsonPath: var string)
first type mismatch at position: 1
required type for dst: var T: distinct
but expression 'dst.source' is of type: openArray[oSource]
proc initFromJson[T: enum](dst: var T; jsonNode: JsonNode; jsonPath: var string)
first type mismatch at position: 1
required type for dst: var T: enum
but expression 'dst.source' is of type: openArray[oSource]
proc initFromJson[T: object | tuple](dst: var T; jsonNode: JsonNode; jsonPath: var string)
first type mismatch at position: 1
required type for dst: var T: object or tuple
but expression 'dst.source' is of type: openArray[oSource]
proc initFromJson[T](dst: var Option[T]; jsonNode: JsonNode; jsonPath: var string)
first type mismatch at position: 1
required type for dst: var Option[initFromJson.T]
but expression 'dst.source' is of type: openArray[oSource]
proc initFromJson[T](dst: var OrderedTable[string, T]; jsonNode: JsonNode;
jsonPath: var string)
first type mismatch at position: 1
required type for dst: var OrderedTable[system.string, initFromJson.T]
but expression 'dst.source' is of type: openArray[oSource]
proc initFromJson[T](dst: var Table[string, T]; jsonNode: JsonNode;
jsonPath: var string)
first type mismatch at position: 1
required type for dst: var Table[system.string, initFromJson.T]
but expression 'dst.source' is of type: openArray[oSource]
proc initFromJson[T](dst: var ref T; jsonNode: JsonNode; jsonPath: var string)
first type mismatch at position: 1
required type for dst: var ref T
but expression 'dst.source' is of type: openArray[oSource]
proc initFromJson[T](dst: var seq[T]; jsonNode: JsonNode; jsonPath: var string)
first type mismatch at position: 1
required type for dst: var seq[T]
but expression 'dst.source' is of type: openArray[oSource]
Alles anzeigen
In einem ersten Versuch hatte ich statt "openArray[string]" mit Sequences gearbeitet. Nur schlägt auch das fehl, weil der JSON-Parser wohl erkennt, wenn Daten in einem Array vorliegen und die Übernahme in eine Sequenz ablehnt. - Fehlermeldung (Compile) war in etwa: "Type is seq[string] but array found".
Wie komme ich nun am besten aus der Situation raus?
Momentan fiele mir nur ein: Statt "openArray" mit "array[string,FIXE-ZAHL]" zu arbeiten und alle Einträge in der JSON-Datei entsprechend mit Leerwerten auffüllen.
Also wenn ihr Ideen habt: Gern her damit.
EDIT:
OK, in der Beschreibung zur Funktion to steht es auch
ZitatKnown limitations:
Heterogeneous arrays are not supported.
Dann muss ich mich wohl doch von Node zu Node hangeln.
EDIT #2:
Hmm, ein weiteres Problem
Mit dem JSON-String aus obigem Code
Führt zum Fehler bei der Ausführung:
C:\Users\BugFix\scoop\apps\nim\1.2.0\lib\pure\json.nim(931) parseJson
C:\Users\BugFix\scoop\apps\nim\1.2.0\lib\pure\json.nim(847) parseJson
C:\Users\BugFix\scoop\apps\nim\1.2.0\lib\pure\json.nim(806) parseJson
C:\Users\BugFix\scoop\apps\nim\1.2.0\lib\pure\json.nim(806) parseJson
C:\Users\BugFix\scoop\apps\nim\1.2.0\lib\pure\json.nim(815) parseJson
C:\Users\BugFix\scoop\apps\nim\1.2.0\lib\pure\json.nim(806) parseJson
C:\Users\BugFix\scoop\apps\nim\1.2.0\lib\pure\json.nim(818) parseJson
C:\Users\BugFix\scoop\apps\nim\1.2.0\lib\pure\parsejson.nim(526) eat
C:\Users\BugFix\scoop\apps\nim\1.2.0\lib\pure\parsejson.nim(522) raiseParseErr
Error: unhandled exception: input(36, 15) Error: ] expected [JsonParsingError]
Die Zeile, bevor der Error geworfen wird ist: eat(p, tkBracketRi)
Nach meinem Verständnis ein Fehler: Schließende Klammer nicht gefunden. Aber die Klammerzuordnung ist korrekt im JSON-String.
Großes