Code Monkey home page Code Monkey logo

myjson's Introduction

myJSON

JSON parser for delphi 7 and earlier (and maybe even later)

Types:

  • myJSONItem - JSON node
  • myJDType - type of node (Object/Array/Value)
  • myJVType - value's subtype (Text/Number/Boolean)

myJSONItem

properties

  • 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 child
  • Has[name]: boolean - returns true if node has a child with specified name

methods

  • Count: integer - Returns count of child nodes (works for both arrays and objects)
  • Remove(n) - Removes N-th child
  • LoadFromFile / SaveToFile - Obviously
  • Clear - Removes all child nodes, won't work on leaf nodes
  • setArray(newLength: integer) - Explicitly sets type to dtArray and allocates the memory

getters and setters:

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 - returns true if value is explicitly set to null
  • setNull - explicitly sets value to null

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)

Example 1: Reading values

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;

Example 2: Assigning values

...
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"}}

myjson's People

Contributors

badunius avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.