-
Notifications
You must be signed in to change notification settings - Fork 90
/
Grijjy.Bson.IO.pas
7439 lines (6383 loc) · 209 KB
/
Grijjy.Bson.IO.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit Grijjy.Bson.IO;
(*< JSON and BSON reading and writing.
@bold(Quick Start)
Consider this JSON document:
@preformatted(
{ "x" : 1,
"y" : 2,
"z" : [ 3.14, true] }
)
You can serialize it manually to BSON like this:
<source>
var
Writer: IgoBsonWriter;
Bson: TBytes;
begin
Writer := TgoBsonWriter.Create;
Writer.WriteStartDocument;
Writer.WriteName('x');
Writer.WriteInt32(1);
Writer.WriteInt32('y', 2); // Writes name and value in single call
Writer.WriteName('z');
Writer.WriteStartArray;
Writer.WriteDouble(3.14);
Writer.WriteBoolean(True);
Writer.WriteEndArray;
Writer.WriteEndDocument;
Bson := Writer.ToBson;
end;
</source>
Likewise, you can serialize to JSON by using the IgoJsonWriter interface
instead.
You can also manually deserialize by using the IgoBsonReader and IgoJsonReader
interfaces. However, these are a bit more complicated to use since you don't
know the deserialized BSON types in advance.
You can look at the unit tests in the unit Grijjy.Data.Bson.IO.Tests
for examples of manual serialization and deserialization. *)
{$INCLUDE 'Grijjy.inc'}
interface
uses
System.Classes,
System.SysUtils,
Grijjy.Bson;
type
{ Type of exception that is raised when parsing an invalid JSON source. }
EgoJsonParserError = class(Exception)
{$REGION 'Internal Declarations'}
private
FLineNumber: Integer;
FColumnNumber: Integer;
FPosition: Integer;
{$WARNINGS OFF}
private
constructor Create(const AMsg: String; const ALineNumber,
AColumnNumber, APosition: Integer);
{$WARNINGS ON}
{$ENDREGION 'Internal Declarations'}
public
{ The line number of the error in the source text, starting at 1. }
property LineNumber: Integer read FLineNumber;
{ The column number of the error in the source text, starting at 1. }
property ColumnNumber: Integer read FColumnNumber;
{ The position of the error in the source text, starting at 0.
The position is the offset (in characters) from the beginning of the
text. }
property Position: Integer read FPosition;
end;
type
{ State of a IgoBsonBaseWriter }
TgoBsonWriterState = (
{ Initial state }
Initial,
{ The writer is positioned to write a name }
Name,
{ The writer is positioned to write a value }
Value,
{ The writer is positioned to write a scope document (call
WriteStartDocument to start writing the scope document). }
ScopeDocument,
{ The writer is done }
Done,
{ The writer is closed }
Closed);
type
{ State of a IgoBsonBaseReader }
TgoBsonReaderState = (
{ Initial state }
Initial,
{ The reader is positioned at the type of an element or value }
&Type,
{ The reader is positioned at the name of an element }
Name,
{ The reader is positioned at the value }
Value,
{ The reader is positioned at a scope document }
ScopeDocument,
{ The reader is positioned at the end of a document }
EndOfDocument,
{ The reader is positioned at the end of an array }
EndOfArray,
{ The reader has finished reading a document }
Done,
{ The reader is closed }
Closed);
type
{ Used internally by BSON/JSON readers and writers to represent the current
context. }
TgoBsonContextType = (
{ The top level of a BSON document }
TopLevel,
{ A (possible embedded) BSON document }
Document,
{ A BSON array }
&Array,
{ A JavaScript w/Scope BSON value }
JavaScriptWithScope,
{ The scope document of a JavaScript w/Scope BSON value }
ScopeDocument);
type
{ Base interface for IgoBsonWriter, IgoJsonWriter and IgoBsonDocumentWriter }
IgoBsonBaseWriter = interface
['{4525DC0D-C54E-47B2-85BE-4C09A8F5DF54}']
{$REGION 'Internal Declarations'}
function GetState: TgoBsonWriterState;
{$ENDREGION 'Internal Declarations'}
{ Writes a BSON value.
Parameters:
AValue: the BSON value to write.
Raises:
EArgumentNilException if AValue has not been assigned (IsNil returns
True). }
procedure WriteValue(const AValue: TgoBsonValue);
{ Writes a BSON binary.
Parameters:
AValue: the BSON binary to write.
Raises:
EArgumentNilException if AValue has not been assigned (IsNil returns
True). }
procedure WriteBinaryData(const AValue: TgoBsonBinaryData);
{ Writes a BSON Regular Expression.
Parameters:
AValue: the BSON Regular Expression to write.
Raises:
EArgumentNilException if AValue has not been assigned (IsNil returns
True). }
procedure WriteRegularExpression(const AValue: TgoBsonRegularExpression); overload;
{ Writes both an element name and a BSON Regular Expression.
Parameters:
AName: the element name.
AValue: the BSON Regular Expression to write.
Raises:
EArgumentNilException if AValue has not been assigned (IsNil returns
True). }
procedure WriteRegularExpression(const AName: String; const AValue: TgoBsonRegularExpression); overload;
{ Writes the name of an element.
Parameters:
AName: the element name. }
procedure WriteName(const AName: String);
{ Writes a Boolean value.
Parameters:
AValue: the Boolean value. }
procedure WriteBoolean(const AValue: Boolean); overload;
{ Writes a name/value pair with a Boolean value.
Can only be used when inside a document.
Parameters:
AName: the element name.
AValue: the Boolean value. }
procedure WriteBoolean(const AName: String; const AValue: Boolean); overload;
{ Writes a 32-bit Integer value.
Parameters:
AValue: the Integer value. }
procedure WriteInt32(const AValue: Integer); overload;
{ Writes a name/value pair with a 32-bit Integer value.
Can only be used when inside a document.
Parameters:
AName: the element name.
AValue: the Integer value. }
procedure WriteInt32(const AName: String; const AValue: Integer); overload;
{ Writes a 64-bit Integer value.
Parameters:
AValue: the Integer value. }
procedure WriteInt64(const AValue: Int64); overload;
{ Writes a name/value pair with a 64-bit Integer value.
Can only be used when inside a document.
Parameters:
AName: the element name.
AValue: the Integer value. }
procedure WriteInt64(const AName: String; const AValue: Int64); overload;
{ Writes a Double value.
Parameters:
AValue: the Double value. }
procedure WriteDouble(const AValue: Double); overload;
{ Writes a name/value pair with a Double value.
Can only be used when inside a document.
Parameters:
AName: the element name.
AValue: the Double value. }
procedure WriteDouble(const AName: String; const AValue: Double); overload;
{ Writes a String value.
Parameters:
AValue: the String value. }
procedure WriteString(const AValue: String); overload;
{ Writes a name/value pair with a String value.
Can only be used when inside a document.
Parameters:
AName: the element name.
AValue: the String value. }
procedure WriteString(const AName, AValue: String); overload;
{ Writes a DateTime value.
Parameters:
AMillisecondsSinceEpoch: the number of UTC milliseconds since the
Unix epoch }
procedure WriteDateTime(const AMillisecondsSinceEpoch: Int64); overload;
{ Writes a name/value pair with a DateTime value.
Can only be used when inside a document.
Parameters:
AName: the element name.
AMillisecondsSinceEpoch: the number of UTC milliseconds since the
Unix epoch }
procedure WriteDateTime(const AName: String; const AMillisecondsSinceEpoch: Int64); overload;
{ Writes a byte array as binary data of sub type Binary
Parameters:
ABytes: the bytes to write }
procedure WriteBytes(const AValue: TBytes); overload;
{ Writes a name/value pair with binary date.
Can only be used when inside a document.
Parameters:
AName: the element name.
ABytes: the bytes to write }
procedure WriteBytes(const AName: String; const AValue: TBytes); overload;
{ Writes a Timestamp value.
Parameters:
AValue: the Timestamp value. }
procedure WriteTimestamp(const AValue: Int64); overload;
{ Writes a name/value pair with a Timestamp value.
Can only be used when inside a document.
Parameters:
AName: the element name.
AValue: the Timestamp value. }
procedure WriteTimestamp(const AName: String; const AValue: Int64); overload;
{ Writes an ObjectId value.
Parameters:
AValue: the ObjectId value. }
procedure WriteObjectId(const AValue: TgoObjectId); overload;
{ Writes a name/value pair with an ObjectId value.
Can only be used when inside a document.
Parameters:
AName: the element name.
AValue: the ObjectId value. }
procedure WriteObjectId(const AName: String; const AValue: TgoObjectId); overload;
{ Writes a JavaScript.
Parameters:
ACode: the JavaScript code. }
procedure WriteJavaScript(const ACode: String); overload;
{ Writes a name/value pair with a JavaScript.
Can only be used when inside a document.
Parameters:
AName: the element name.
ACode: the JavaScript code. }
procedure WriteJavaScript(const AName, ACode: String); overload;
{ Writes a JavaScript with scope.
Parameters:
ACode: the JavaScript code.
@bold(Note): call WriteStartDocument to start writing the scope. }
procedure WriteJavaScriptWithScope(const ACode: String); overload;
{ Writes a name/value pair with a JavaScript with scope.
Can only be used when inside a document.
Parameters:
AName: the element name.
ACode: the JavaScript code.
@bold(Note): call WriteStartDocument to start writing the scope. }
procedure WriteJavaScriptWithScope(const AName, ACode: String); overload;
{ Writes a BSON Null value }
procedure WriteNull; overload;
{ Writes a name/value pair with a BSON Null value.
Can only be used when inside a document.
Parameters:
AName: the element name. }
procedure WriteNull(const AName: String); overload;
{ Writes a BSON Undefined value. }
procedure WriteUndefined; overload;
{ Writes a name/value pair with a BSON Undefined value.
Can only be used when inside a document.
Parameters:
AName: the element name. }
procedure WriteUndefined(const AName: String); overload;
{ Writes a BSON MaxKey value }
procedure WriteMaxKey; overload;
{ Writes a name/value pair with a BSON MaxKey value.
Can only be used when inside a document.
Parameters:
AName: the element name. }
procedure WriteMaxKey(const AName: String); overload;
{ Writes a BSON MinKey value }
procedure WriteMinKey; overload;
{ Writes a name/value pair with a BSON MinKey value.
Can only be used when inside a document.
Parameters:
AName: the element name. }
procedure WriteMinKey(const AName: String); overload;
{ Writes a BSON Symbol.
Parameters:
AValue: the symbol. }
procedure WriteSymbol(const AValue: String); overload;
{ Writes a name/value pair with a BSON Symbol.
Can only be used when inside a document.
Parameters:
AName: the element name.
AValue: the symbol. }
procedure WriteSymbol(const AName, AValue: String); overload;
{ Writes the start of a BSON Array }
procedure WriteStartArray; overload;
{ Writes a name/value pair, where the value starts a BSON Array.
Can only be used when inside a document.
Parameters:
AName: the element name. }
procedure WriteStartArray(const AName: String); overload;
{ Writes the end of a BSON Array }
procedure WriteEndArray;
{ Writes the start of a BSON Document }
procedure WriteStartDocument; overload;
{ Writes a name/value pair, where the value starts a BSON Document.
Can only be used when inside a document.
Parameters:
AName: the element name. }
procedure WriteStartDocument(const AName: String); overload;
{ Writes the end of a BSON Document }
procedure WriteEndDocument;
{ The current state of the writer }
property State: TgoBsonWriterState read GetState;
end;
type
{ Interface for writing BSON values to binary BSON format.
See TgoBsonWriter for the stock implementation of this interface. }
IgoBsonWriter = interface(IgoBsonBaseWriter)
['{6B413B69-018F-48AD-8D81-140C1078AFA1}']
{ Returns the currently written data as a byte array.
Returns:
The data in BSON format.
@bold(Note): you usually call this method when you have finished writing
a BSON Document or value }
function ToBson: TBytes;
{ Writes a raw BSON document.
Parameters:
AValue: the raw BSON document to write.
@bold(Note): no BSON validity checking is performed. The value will be
written as-is, and generate invalid BSON of not used carefully. }
procedure WriteRawBsonDocument(const ADocument: TBytes);
end;
type
{ Interface for writing BSON values to JSON format.
See TgoJsonWriter for the stock implementation of this interface. }
IgoJsonWriter = interface(IgoBsonBaseWriter)
['{92F5BA20-02C9-401C-8403-B51F8898E692}']
{ Returns the currently written data as a JSON string.
Returns:
The data in JSON format.
@bold(Note): you usually call this method when you have finished writing
a BSON Document or value }
function ToJson: String;
{ Inserts a raw value into the current JSON string.
Parameters:
AValue: the value to write.
@bold(Note): no JSON syntax checking is performed. The value will be
written as-is, and generate invalid JSON of not used carefully. You
usually never call this method yourself. }
procedure WriteRaw(const AValue: String);
end;
type
{ Interface for writing BSON values to a BSON document.
See TgoBsonDocumentWriter for the stock implementation of this interface. }
IgoBsonDocumentWriter = interface(IgoBsonBaseWriter)
['{4A410F7E-69FA-46A0-ACE2-317AF5DEA2B8}']
{$REGION 'Internal Declarations'}
function GetDocument: TgoBsonDocument;
{$ENDREGION 'Internal Declarations'}
{ The document the writer writes to }
property Document: TgoBsonDocument read GetDocument;
end;
type
{ A bookmark that can be used to return a reader to the current position and
state. }
IgoBsonReaderBookmark = interface
['{7324A2DE-20F6-4FF2-9973-FD861F6833EB}']
{$REGION 'Internal Declarations'}
function GetState: TgoBsonReaderState;
function GetCurrentBsonType: TgoBsonType;
function GetCurrentName: String;
{$ENDREGION 'Internal Declarations'}
{ The current state of the reader }
property State: TgoBsonReaderState read GetState;
{ The current BsonType }
property CurrentBsonType: TgoBsonType read GetCurrentBsonType;
{ The name of the current element }
property CurrentName: String read GetCurrentName;
end;
type
{ Base interface for IgoBsonReader, IgoJsonReader and IgoBsonDocumentReader }
IgoBsonBaseReader = interface
['{A0592C3C-5E24-4424-9662-EA7F33BB1B9A}']
{$REGION 'Internal Declarations'}
function GetState: TgoBsonReaderState;
{$ENDREGION 'Internal Declarations'}
{ Whether the reader is at the end of the stream.
Returns:
True if at end of stream }
function EndOfStream: Boolean;
{ Gets the current BSON type in the stream.
Returns:
The current BSON type.
@bold(Note): calls ReadBsonType if necessary. }
function GetCurrentBsonType: TgoBsonType;
{ Gets a bookmark to the reader's current position and state.
Returns:
A bookmark.
You can use the returned bookmark to restore the state using
ReturnToBookmark. }
function GetBookmark: IgoBsonReaderBookmark;
{ Returns the reader to previously bookmarked position and state.
AParameters:
ABookmark: the bookmark to return to. This value has previously been
acquired using GetBookmark. }
procedure ReturnToBookmark(const ABookmark: IgoBsonReaderBookmark);
{ Reads a BSON Document from the stream.
Returns:
The read BSON Document.
Raises:
An exception if the current position in the stream does not contain
a BSON Document, or the stream is invalid. }
function ReadDocument: TgoBsonDocument;
{ Reads a BSON Array from the stream.
Returns:
The read BSON Array.
Raises:
An exception if the current position in the stream does not contain
a BSON Array, or the stream is invalid. }
function ReadArray: TgoBsonArray;
{ Reads a BSON value from the stream.
Returns:
The read BSON value.
Raises:
An exception if the current position in the stream does not contain
a BSON value, or the stream is invalid. }
function ReadValue: TgoBsonValue;
{ Reads a BSON Binary from the stream.
Returns:
The read BSON Binary.
Raises:
An exception if the current position in the stream does not contain
a BSON Binary, or the stream is invalid. }
function ReadBinaryData: TgoBsonBinaryData;
{ Reads a BSON Regular Expression from the stream.
Returns:
The read BSON Regular Expression.
Raises:
An exception if the current position in the stream does not contain
a BSON Regular Expression, or the stream is invalid. }
function ReadRegularExpression: TgoBsonRegularExpression;
{ Reads a BSON type from the stream.
Returns:
The read BSON type.
Raises:
An exception if the current position in the stream does not contain
a BSON type, or the stream is invalid. }
function ReadBsonType: TgoBsonType;
{ Reads the name of an element from the stream.
Returns:
The read element name.
Raises:
An exception if the current position in the stream does not contain
an element name, or the stream is invalid. }
function ReadName: String;
{ Skips the name of an element.
Raises:
An exception if the current position in the stream does not contain
an element name, or the stream is invalid. }
procedure SkipName;
{ Skips the value of an element.
Raises:
An exception if the current position in the stream does not contain
an element value, or the stream is invalid. }
procedure SkipValue;
{ Reads a Boolean value from the stream.
Returns:
The read Boolean value.
Raises:
An exception if the current position in the stream does not contain
a Boolean value, or the stream is invalid. }
function ReadBoolean: Boolean;
{ Reads a 32-bit Integer value from the stream.
Returns:
The read Integer value.
Raises:
An exception if the current position in the stream does not contain
a 32-bit Integer value, or the stream is invalid. }
function ReadInt32: Integer;
{ Reads a 64-bit Integer value from the stream.
Returns:
The read Integer value.
Raises:
An exception if the current position in the stream does not contain
a 64-bit Integer value, or the stream is invalid. }
function ReadInt64: Int64;
{ Reads a Double value from the stream.
Returns:
The read Double value.
Raises:
An exception if the current position in the stream does not contain
a Double value, or the stream is invalid. }
function ReadDouble: Double;
{ Reads a String value from the stream.
Returns:
The read String value.
Raises:
An exception if the current position in the stream does not contain
a String value, or the stream is invalid. }
function ReadString: String;
{ Reads a DateTime value from the stream.
Returns:
The read DateTime value as the number of UTC milliseconds since the
Unix epoch.
Raises:
An exception if the current position in the stream does not contain
a DateTime value, or the stream is invalid. }
function ReadDateTime: Int64;
{ Reads a Timestamp value from the stream.
Returns:
The read Timestamp value.
Raises:
An exception if the current position in the stream does not contain
a Timestamp value, or the stream is invalid. }
function ReadTimestamp: Int64;
{ Reads an ObjectId value from the stream.
Returns:
The read ObjectId value.
Raises:
An exception if the current position in the stream does not contain
a ObjectId value, or the stream is invalid. }
function ReadObjectId: TgoObjectId;
{ Reads a Binary value from the stream as a byte array.
Returns:
The read Binary value.
Raises:
An exception if the current position in the stream does not contain
a BSON Binary, or the stream is invalid. }
function ReadBytes: TBytes;
{ Reads a JavaScript from the stream.
Returns:
The read JavaScript.
Raises:
An exception if the current position in the stream does not contain
a JavaScript, or the stream is invalid. }
function ReadJavaScript: String;
{ Reads a JavaScript with scope from the stream.
Returns:
The read JavaScript.
Raises:
An exception if the current position in the stream does not contain
a JavaScript with Scope, or the stream is invalid.
@bold(Note): call ReadStartDocument next to read the scope. }
function ReadJavaScriptWithScope: String;
{ Reads a BSON Null value from the stream.
Raises:
An exception if the current position in the stream does not contain
a Null value, or the stream is invalid. }
procedure ReadNull;
{ Reads a BSON Undefined value from the stream.
Raises:
An exception if the current position in the stream does not contain
a Undefined value, or the stream is invalid. }
procedure ReadUndefined;
{ Reads a BSON MaxKey value from the stream.
Raises:
An exception if the current position in the stream does not contain
a MaxKey value, or the stream is invalid. }
procedure ReadMaxKey;
{ Reads a BSON MinKey value from the stream.
Raises:
An exception if the current position in the stream does not contain
a MinKey value, or the stream is invalid. }
procedure ReadMinKey;
{ Reads a BSON Symbol from the stream.
Returns:
The read Symbol name.
Raises:
An exception if the current position in the stream does not contain
a Symbol, or the stream is invalid. }
function ReadSymbol: String;
{ Reads the start of a BSON Array from the stream.
Raises:
An exception if the current position in the stream does not contain
the start of a BSON Array, or the stream is invalid. }
procedure ReadStartArray;
{ Reads the end of a BSON Array from the stream.
Raises:
An exception if the current position in the stream does not contain
the end of a BSON Array, or the stream is invalid. }
procedure ReadEndArray;
{ Reads the start of a BSON Document from the stream.
Raises:
An exception if the current position in the stream does not contain
the start of a BSON Document, or the stream is invalid. }
procedure ReadStartDocument;
{ Reads the end of a BSON Document from the stream.
Raises:
An exception if the current position in the stream does not contain
the end of a BSON Document, or the stream is invalid. }
procedure ReadEndDocument;
{ The current state of the reader }
property State: TgoBsonReaderState read GetState;
end;
type
{ Interface for reading BSON values from binary BSON format.
See TgoBsonReader for the stock implementation of this interface. }
IgoBsonReader = interface(IgoBsonBaseReader)
['{773A4BBE-A4D9-4DDA-A937-C865ADC0A5B8}']
end;
type
{ Interface for reading BSON values from JSON format.
See TgoJsonReader for the stock implementation of this interface. }
IgoJsonReader = interface(IgoBsonBaseReader)
['{F579A93F-760C-463D-9B54-64AAF527F514}']
end;
type
{ Interface for reading BSON values from a BSON Document.
See TgoBsonDocumentReader for the stock implementation of this interface. }
IgoBsonDocumentReader = interface(IgoBsonBaseReader)
['{1D4F90C4-C790-491C-844A-C7FFCF58F2E8}']
end;
type
{ Abstract base class of TgoBsonWriter and TgoJsonWriter.
Implements the IgoBsonBaseWriter interface. }
TgoBsonBaseWriter = class abstract(TInterfacedObject, IgoBsonBaseWriter)
{$REGION 'Internal Declarations'}
private
FState: TgoBsonWriterState;
FName: String;
private
procedure WriteValueIntf(const AValue: TgoBsonValue._IValue);
procedure WriteArray(const AArray: TgoBsonArray._IArray);
procedure WriteDocument(const ADocument: TgoBsonDocument._IDocument);
procedure DoWriteBinaryData(const AValue: TgoBsonValue._IValue);
procedure DoWriteDateTime(const AValue: TgoBsonValue._IValue);
procedure DoWriteRegularExpression(const AValue: TgoBsonValue._IValue);
procedure DoWriteJavaScript(const AValue: TgoBsonValue._IValue);
procedure DoWriteJavaScriptWithScope(const AValue: TgoBsonJavaScriptWithScope); overload;
procedure DoWriteJavaScriptWithScope(const AValue: TgoBsonValue._IValue); overload;
procedure DoWriteSymbol(const AValue: TgoBsonValue._IValue);
procedure DoWriteTimestamp(const AValue: TgoBsonValue._IValue);
protected
{ IgoBsonBaseWriter }
procedure WriteName(const AName: String); virtual;
procedure WriteValue(const AValue: TgoBsonValue);
function GetState: TgoBsonWriterState;
procedure WriteBoolean(const AValue: Boolean); overload; virtual; abstract;
procedure WriteBoolean(const AName: String; const AValue: Boolean); overload;
procedure WriteInt32(const AValue: Integer); overload; virtual; abstract;
procedure WriteInt32(const AName: String; const AValue: Int32); overload;
procedure WriteInt64(const AValue: Int64); overload; virtual; abstract;
procedure WriteInt64(const AName: String; const AValue: Int64); overload;
procedure WriteDouble(const AValue: Double); overload; virtual; abstract;
procedure WriteDouble(const AName: String; const AValue: Double); overload;
procedure WriteString(const AValue: String); overload; virtual; abstract;
procedure WriteString(const AName, AValue: String); overload;
procedure WriteDateTime(const AMillisecondsSinceEpoch: Int64); overload; virtual; abstract;
procedure WriteDateTime(const AName: String; const AMillisecondsSinceEpoch: Int64); overload;
procedure WriteBytes(const AValue: TBytes); overload;
procedure WriteBytes(const AName: String; const AValue: TBytes); overload;
procedure WriteTimestamp(const AValue: Int64); overload; virtual; abstract;
procedure WriteTimestamp(const AName: String; const AValue: Int64); overload;
procedure WriteObjectId(const AValue: TgoObjectId); overload; virtual; abstract;
procedure WriteObjectId(const AName: String; const AValue: TgoObjectId); overload;
procedure WriteJavaScript(const ACode: String); overload; virtual; abstract;
procedure WriteJavaScript(const AName, ACode: String); overload;
procedure WriteJavaScriptWithScope(const ACode: String); overload; virtual; abstract;
procedure WriteJavaScriptWithScope(const AName, ACode: String); overload;
procedure WriteNull; overload; virtual; abstract;
procedure WriteNull(const AName: String); overload;
procedure WriteUndefined; overload; virtual; abstract;
procedure WriteUndefined(const AName: String); overload;
procedure WriteMaxKey; overload; virtual; abstract;
procedure WriteMaxKey(const AName: String); overload;
procedure WriteMinKey; overload; virtual; abstract;
procedure WriteMinKey(const AName: String); overload;
procedure WriteSymbol(const AValue: String); overload; virtual; abstract;
procedure WriteSymbol(const AName, AValue: String); overload;
procedure WriteStartArray; overload; virtual; abstract;
procedure WriteStartArray(const AName: String); overload;
procedure WriteEndArray; virtual; abstract;
procedure WriteStartDocument; overload; virtual; abstract;
procedure WriteStartDocument(const AName: String); overload;
procedure WriteEndDocument; virtual; abstract;
procedure WriteBinaryData(const AValue: TgoBsonBinaryData); virtual; abstract;
procedure WriteRegularExpression(const AValue: TgoBsonRegularExpression); overload; virtual; abstract;
procedure WriteRegularExpression(const AName: String; const AValue: TgoBsonRegularExpression); overload;
protected
property State: TgoBsonWriterState read FState write FState;
property Name: String read FName;
{$ENDREGION 'Internal Declarations'}
end;
type
{ Stock implementation of the IgoBsonWriter interface. }
TgoBsonWriter = class(TgoBsonBaseWriter, IgoBsonWriter)
{$REGION 'Internal Declarations'}
private type
TOutput = record
private const
TEMP_BYTES_LENGTH = 128;
private
FBuffer: TBytes;
FSize: Integer;
FCapacity: Integer;
FTempBytes: TBytes;
public
procedure Initialize;
procedure Write(const AValue; const ASize: Integer);
procedure WriteBsonType(const ABsonType: TgoBsonType); inline;
procedure WriteBinarySubType(const ASubType: TgoBsonBinarySubType); inline;
procedure WriteByte(const AValue: Byte); inline;
procedure WriteBoolean(const AValue: Boolean); inline;
procedure WriteInt32(const AValue: Int32); inline;
procedure WriteInt32At(const APosition, AValue: Int32); inline;
procedure WriteInt64(const AValue: Int64); inline;
procedure WriteDouble(const AValue: Double); inline;
procedure WriteCString(const AValue: String); overload;
procedure WriteCString(const AValue: TBytes); overload;
procedure WriteString(const AValue: String);
procedure WriteObjectId(const AValue: TgoObjectId);
function ToBytes: TBytes;
property Position: Integer read FSize;
end;
private type
PContext = ^TContext;
TContext = record
private
FStartPosition: Integer;
FIndex: Integer;
FContextType: TgoBsonContextType;
public
procedure Initialize(const AContextType: TgoBsonContextType;
const AStartPosition: Integer); inline;
property StartPosition: Integer read FStartPosition;
property Index: Integer read FIndex write FIndex;
property ContextType: TgoBsonContextType read FContextType;
end;
protected type
TArrayElementNameAccelerator = record
private class var
FCachedElementNames: array [0..999] of TBytes;
private
class function CreateElementNameBytes(const AIndex: Integer): TBytes; static;
public
class constructor Create;
public
class function GetElementNameBytes(const AIndex: Integer): TBytes; static;
end;
private
FOutput: TOutput;
FContextStack: TArray<TContext>;