JSON parser for delphi 7 and earlier (and maybe even later)
Due to my pathalogic lazyness I strongly recommend you to use https://github.com/hydrobyte/McJSON instead. As of right now my main focus is Godot and TypeScript, therefore, I'm afraid this repo maintainance is at low priority. Feel free to fork this repo if you feel like it, it's under MIT license after all. Be good, do good.
myJSONItem
- JSON nodemyJDType
- type of node (Object/Array/Value)myJVType
- value's subtype (Text/Number/Boolean)
Name: string
- Node name (read only)Item[name]: myJSONItem
- Sub-item with specified name (read only, default property). Creates an item if it doesn't exist yet.Code: string
- Encoded JSON of this node and all of it's child nodes. Assign encoded JSON to this property to parse it.Key[index]: string
- Names of this node's children (won't work for arrays)Value[index]: myJSONItem
- Values of this node's childHas[name]: boolean
- returnstrue
if node has a child with specified name
Count: integer
- Returns count of child nodes (works for both arrays and objects)Remove(n)
- Removes N-th childLoadFromFile / SaveToFile
- ObviouslyClear
- Removes all child nodes, won't work on leaf nodessetArray(newLength: integer)
- Explicitly sets type to dtArray and allocates the memory
All values are stored as strings, therefore there are number of getters to do all conversion for you. Each of this getters lets you to specify default values. Each setter will convert values to string.
- getStr / setStr
// string
- getInt / setInt
// integer
- getNum / setNum
// double (float)
- getBool / setBool
// boolean. True: 'true' or any number > 0. False: 'false' (or any other string, actually) or any number <= 0.
There are also methods for working with null-values
isNull: boolean
- returnstrue
if value is explicitly set tonull
setNull
- explicitly sets value tonull
As it turned out assigning Code
to Code
works well for root elements, or when you assign root element's code to some child element.
However, when you try to clone children this way, Code
getter will return it with the leading "key":
, and when the receiver will parse it, it'll see "key"
and treat it as if it was a text value. Therefore I added (there were other options though) a method for reading value as an encoded JSON string
getJSON: string
- returns value as an encoded JSON string (without adding key to it even if there is one)
conf.json
{
"window": {
"width": 400,
"height": 300
},
"fonts": [
"Arial",
"Tahoma"
]
}
test1.pas
...
config := myJSONItem.Create;
list := TStringList.Create;
...
config.LoadFromFile('conf.json');
wnd.Width := config['window']['width'].getInt(DEFAULT_WIDTH);
for i := 0 to config['fonts'].Count - 1 do
list.Add(config['fonts'].Value[i].getStr);
...
config.Free;
...
a := myJSONItem.Create;
b := myJSONItem.Create;
a.Code := '{"item1":"value 1","item2":[3,4,5]}';
a['item3'].setStr('value 3');
Writeln(a.Code); // {"item1":"value 1","item2":[3,4,5],"item3":"value 3"}
b['desc'].setStr('And now for something completelly different');
b['c'].Code := a.Code;
Writeln(b.Code); // {"desc":"And now for something completelly different","c":{"item1":"value 1","item2":[3,4,5],"item3":"value 3"}}