You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+64-17Lines changed: 64 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,61 +6,108 @@ _jsonDoc_ started out as _bsonDoc.pas_ and `IBSONDocument` in the [TMongoWire](h
6
6
7
7
### JSON
8
8
9
+
To create a new `IJSONDocument` instance:
10
+
9
11
function JSON: IJSONDocument; overload;
10
12
11
-
Use this function to create a new `IJSONDocument` instance.
13
+
To create and populate a new `IJSONDocument` instance:
12
14
13
15
function JSON(const x: array of OleVariant): IJSONDocument; overload;
14
16
15
-
Convert a variant array into an `IJSONDocument` instance. Pass a list of key/value-pairs, use value `[` to start an embedded document, and key `]` to close it.
17
+
Pass a list of alternately keys and values, suffix the key with `{` to start an embedded document, and use key `}` to close it. E.g.: with
`d.ToString` will return `{"a":1,"b":{"x":"foo"},"c":{"x":"bar"},"d":[{"x":"hello"},{"x":"world"}],"e":true}`.
22
+
23
+
Convert a variant into an IJSONDocument reference.
16
24
17
25
function JSON(x: OleVariant): IJSONDocument; overload;
18
26
19
-
Use this overload to convert an OleVariant into an `IJSONDocument` reference:
27
+
Depending on the value of the argument:
20
28
21
29
* string types are parsed into a new `IJSONDocument` instance,
22
30
* Null (or empty or unassigned) returns `nil`,
23
31
* in all other cases the variant is probed for a reference to an `IJSONDocument` instace.
24
32
25
33
### IJSONDocument
26
34
27
-
function Parse(const JSONData: WideString): IJSONDocument; safecall;
35
+
Merge a string with JSON data into the `IJSONDocument` instance, existing keys will get their values overwritten (see `Clear` below).
28
36
29
-
Convert a string with JSON data into the `IJSONDocument` instance.
30
-
31
-
function ToString: WideString; safecall;
37
+
function Parse(const JSONData: WideString): IJSONDocument;
32
38
33
39
Convert the data in the `IJSONDocument` instance into a JSON string.
34
40
35
-
function ToVarArray:OleVariant; safecall;
41
+
function ToString: WideString;
42
+
43
+
Convert the data in the `IJSONDocument` instance into a Variant array of dimensions [0.._n_,0..1], where [_i_,0] holds keys and [_i_,1] holds values.
44
+
45
+
function ToVarArray:OleVariant;
36
46
37
-
Convert the data in the `IJSONDocument` instance into a Variant array.
47
+
Clear the values of the `IJSONDocument`, but keep the list of keys.
38
48
39
-
procedure Clear; safecall;
49
+
procedure Clear;
40
50
41
-
Clear the values of the `IJSONDocument`, but keep the list of keys. When processing a sequence of JSON documents with a similar set of keys (and keys of embedded documents), performance can be gained by avoiding the de- and re-allocation of memory to store the keys (and the Variant record for their values).
51
+
When processing a sequence of JSON documents with a similar set of keys (and keys of embedded documents), performance can be gained by avoiding the de- and re-allocation of memory to store the keys (and the Variant record for their values). (See also `IJSONDocArrayBuilder` below).
52
+
53
+
Retrieve a value by key. This is the default property, so you can access the keys of a `IJSONDocument` by index notation (e.g.: `d['id']`).
Get or set the value for a key. Notice this is the default property, so you can access the keys of a `IJSONDocument` by index notation (e.g.: `d['id']`)
57
+
## Remarks
58
+
59
+
**Attention:** the default `IJSONDocument` implementation: `TJSONDocument` is **not** thread-safe. Please use proper locking and synchronisation methods to ensure only one thread accesses an instance at one time, or use `jsonTS.pas`.
60
+
61
+
### JSONEnum
62
+
63
+
Create an `IJSONEnumerator` instance for a `IJSONDocument` reference.
64
+
65
+
function JSONEnum(x: IJSONDocument): IJSONEnumerator; overload;
66
+
function JSONEnum(const x: OleVariant): IJSONEnumerator; overload;
46
67
47
68
### IJSONEnumerator
48
69
49
-
Use the `JSONEnum` function to create an `IJSONEnumerator` instance for a `IJSONDocument` reference.
70
+
Check wether the enumerator is past the end of the set of keys of the document.
50
71
51
72
function EOF: boolean;
52
73
53
-
Checks wether the enumerator is past the end of the set. Use `EOF` on a new enumerator for skipping the iteration on an empty set.
74
+
Move the iterator to the next item in the set. Moves the iterator to the first item on the first call. Returns false when moved past the end of the set.
54
75
55
76
function Next: boolean;
56
77
57
-
Moves the iterator to the next item in the set. Moves the iterator to the first item on the first call. Returns false when moved past the end of the set.
78
+
Returns the key or value of the current item in the set.
58
79
59
80
function Key: WideString;
60
81
function Value: OleVariant;
61
82
62
83
Returns the key or value of the current item in the set.
63
84
64
-
## Remarks
85
+
### JSONDocArray
86
+
87
+
Use an `IJSONDocArrayBuilder` instance to store a set of similar JSON documents. JSON is converted to and from strings internally to save on memory usage. Use `LoadItem` with a single `IJSONDocument` instance to re-use keys and save on memory allocation. Pre-load a parent `IJSONDocument` with an `IJSONDocArrayBuilder` instance to postpone some of the parsing of the children documents.
88
+
89
+
function JSONDocArray: IJSONDocArrayBuilder; overload;
90
+
function JSONDocArray(const Items:array of IJSONDocument): IJSONDocArrayBuilder; overload;
91
+
92
+
### IJSONDocArrayBuilder
93
+
94
+
Append a document to the array.
95
+
96
+
function Add(Doc: IJSONDocument): integer;
97
+
function AddJSON(const Data: WideString): integer;
98
+
99
+
Retrieve a document using an existing `IJSONDocument` instance, possibly re-using allocated keys.
Convert the data in the `IJSONDocArrayBuilder` instance into a JSON string.
108
+
109
+
function ToString: WideString; stdcall;
110
+
111
+
Retrieve a document by index, in a new `IJSONDocument` instance. This is the default property, so you can use index notation (e.g.: `a[3]`).
65
112
66
-
**Attention:** the default `IJSONDocument` implementation: `TJSONDocument` is **not** thread-safe. Please use proper locking and synchronisation methods to ensure only one thread accesses an instance at one time.
0 commit comments