@@ -128,7 +128,7 @@ JsonValueUnwrap(const JsonValue *val, JsonValue *valbuf)
128
128
129
129
if (jc -> ops == & jsonvContainerOps )
130
130
{
131
- val = (JsonbValue * ) jc -> data ;
131
+ val = (JsonbValue * ) JsonContainerDataPtr ( jc ) ;
132
132
Assert (val -> type != jbvBinary );
133
133
}
134
134
else if (JsonContainerIsScalar (jc ))
@@ -304,7 +304,7 @@ static void
304
304
jsonvInitContainer (JsonContainerData * jc , const JsonValue * val )
305
305
{
306
306
jc -> ops = & jsonvContainerOps ;
307
- jc -> data = (void * ) val ;
307
+ JsonContainerDataPtr ( jc ) = (void * ) val ;
308
308
jc -> len = 0 ;
309
309
jc -> size = val -> type == jbvBinary ? val -> val .binary .data -> size :
310
310
val -> type == jbvObject ? val -> val .object .nPairs :
@@ -322,7 +322,7 @@ JsonValueToContainer(const JsonValue *val)
322
322
return val -> val .binary .data ;
323
323
else
324
324
{
325
- JsonContainerData * jc = JsonContainerAlloc ();
325
+ JsonContainerData * jc = JsonContainerAlloc (& jsonvContainerOps );
326
326
jsonvInitContainer (jc , val );
327
327
return jc ;
328
328
}
@@ -354,7 +354,7 @@ static JsonIteratorToken
354
354
jsonvScalarIteratorNext (JsonIterator * * it , JsonValue * res , bool skipNested )
355
355
{
356
356
JsonvScalarIterator * sit = (JsonvScalarIterator * ) * it ;
357
- JsonValue * val = ( * it )-> container -> data ;
357
+ JsonValue * val = JsonContainerDataPtr (( * it )-> container ) ;
358
358
359
359
Assert (IsAJsonbScalar (val ));
360
360
@@ -384,7 +384,7 @@ static JsonIteratorToken
384
384
jsonvArrayIteratorNext (JsonIterator * * it , JsonValue * res , bool skipNested )
385
385
{
386
386
JsonvArrayIterator * ait = (JsonvArrayIterator * ) * it ;
387
- JsonValue * arr = ( * it )-> container -> data ;
387
+ JsonValue * arr = JsonContainerDataPtr (( * it )-> container ) ;
388
388
JsonValue * val ;
389
389
390
390
Assert (arr -> type == jbvArray );
@@ -428,7 +428,7 @@ static JsonIteratorToken
428
428
jsonvObjectIteratorNext (JsonIterator * * it , JsonValue * res , bool skipNested )
429
429
{
430
430
JsonvObjectIterator * oit = (JsonvObjectIterator * ) * it ;
431
- JsonValue * obj = ( * it )-> container -> data ;
431
+ JsonValue * obj = JsonContainerDataPtr (( * it )-> container ) ;
432
432
JsonPair * pair ;
433
433
434
434
Assert (obj -> type == jbvObject );
@@ -553,13 +553,13 @@ jsonvIteratorInitFromValue(JsonValue *val, JsonContainer *jsc)
553
553
static JsonIterator *
554
554
jsonvIteratorInit (JsonContainer * jsc )
555
555
{
556
- return jsonvIteratorInitFromValue (jsc -> data , jsc );
556
+ return jsonvIteratorInitFromValue (JsonContainerDataPtr ( jsc ) , jsc );
557
557
}
558
558
559
559
static JsonValue *
560
560
jsonvFindKeyInObject (JsonContainer * objc , const char * key , int len )
561
561
{
562
- JsonValue * obj = ( JsonValue * ) objc -> data ;
562
+ JsonValue * obj = JsonContainerDataPtr ( objc ) ;
563
563
JsonValue * res ;
564
564
JsonValue * jv ;
565
565
int i ;
@@ -605,7 +605,7 @@ jsonvFindKeyInObject(JsonContainer *objc, const char *key, int len)
605
605
static JsonValue *
606
606
jsonvFindValueInArray (JsonContainer * arrc , const JsonValue * val )
607
607
{
608
- JsonValue * arr = ( JsonValue * ) arrc -> data ;
608
+ JsonValue * arr = JsonContainerDataPtr ( arrc ) ;
609
609
610
610
Assert (JsonContainerIsArray (arrc ));
611
611
Assert (IsAJsonbScalar (val ));
@@ -640,7 +640,7 @@ jsonvFindValueInArray(JsonContainer *arrc, const JsonValue *val)
640
640
static JsonValue *
641
641
jsonvGetArrayElement (JsonContainer * arrc , uint32 index )
642
642
{
643
- JsonValue * arr = ( JsonValue * ) arrc -> data ;
643
+ JsonValue * arr = JsonContainerDataPtr ( arrc ) ;
644
644
645
645
Assert (JsonContainerIsArray (arrc ));
646
646
@@ -668,7 +668,7 @@ jsonvGetArrayElement(JsonContainer *arrc, uint32 index)
668
668
static uint32
669
669
jsonvGetArraySize (JsonContainer * arrc )
670
670
{
671
- JsonValue * arr = ( JsonValue * ) arrc -> data ;
671
+ JsonValue * arr = JsonContainerDataPtr ( arrc ) ;
672
672
673
673
Assert (JsonContainerIsArray (arrc ));
674
674
@@ -692,17 +692,18 @@ jsonvGetArraySize(JsonContainer *arrc)
692
692
static JsonContainer *
693
693
jsonvCopy (JsonContainer * jc )
694
694
{
695
- JsonContainerData * res = JsonContainerAlloc ();
695
+ JsonContainerData * res = JsonContainerAlloc (& jsonvContainerOps );
696
696
697
697
* res = * jc ;
698
- res -> data = JsonValueCopy (NULL , ( JsonValue * ) jc -> data );
698
+ JsonContainerDataPtr ( res ) = JsonValueCopy (NULL , JsonContainerDataPtr ( jc ) );
699
699
700
700
return res ;
701
701
}
702
702
703
703
JsonContainerOps
704
704
jsonvContainerOps =
705
705
{
706
+ sizeof (JsonValue * ),
706
707
NULL ,
707
708
jsonvIteratorInit ,
708
709
jsonvFindKeyInObject ,
@@ -717,7 +718,7 @@ JsonValue *
717
718
JsonToJsonValue (Json * json , JsonValue * jv )
718
719
{
719
720
if (JsonRoot (json )-> ops == & jsonvContainerOps )
720
- return ( JsonValue * ) JsonRoot (json )-> data ;
721
+ return JsonContainerDataPtr ( JsonRoot (json )) ;
721
722
722
723
if (!jv )
723
724
jv = palloc (sizeof (JsonValue ));
@@ -731,9 +732,9 @@ JsonInit(Json *json)
731
732
const void * data = DatumGetPointer (json -> obj .value );
732
733
const void * detoasted_data ;
733
734
734
- Assert (json -> root . data || data );
735
+ Assert (JsonContainerDataPtr ( & json -> root ) || data );
735
736
736
- if (json -> root . data || !data )
737
+ if (JsonContainerDataPtr ( & json -> root ) || !data ) /* FIXME */
737
738
return ;
738
739
739
740
detoasted_data = PG_DETOAST_DATUM (json -> obj .value );
@@ -750,13 +751,16 @@ JsonExpand(Json *tmp, Datum value, bool freeValue, JsonContainerOps *ops)
750
751
751
752
if (tmp )
752
753
{
754
+ Assert (0 );
753
755
json = tmp ;
754
756
json -> obj .isTemporary = true;
755
757
}
756
758
else
757
759
{
760
+ Size size = JsonAllocSize (ops -> data_size );
761
+
758
762
#ifndef JSON_EXPANDED_OBJECT_MCXT
759
- json = (Json * ) palloc (sizeof ( Json ) );
763
+ json = (Json * ) palloc (size );
760
764
#else
761
765
/*
762
766
* Allocate private context for expanded object. We start by assuming
@@ -770,20 +774,21 @@ JsonExpand(Json *tmp, Datum value, bool freeValue, JsonContainerOps *ops)
770
774
ALLOCSET_SMALL_INITSIZE ,
771
775
ALLOCSET_DEFAULT_MAXSIZE );
772
776
773
- json = (Json * ) MemoryContextAlloc (objcxt , sizeof ( Json ) );
777
+ json = (Json * ) MemoryContextAlloc (objcxt , size );
774
778
#endif
775
779
json -> obj .isTemporary = false;
776
780
}
777
781
778
782
json -> obj .value = value ;
779
783
json -> obj .freeValue = freeValue ;
780
- json -> root .data = NULL ;
781
784
json -> root .len = 0 ;
782
785
json -> root .ops = ops ;
783
786
json -> root .size = -1 ;
784
787
json -> root .type = jbvBinary ;
785
788
json -> is_json = false;
786
789
790
+ memset (json -> root ._data , 0 , ops -> data_size );
791
+
787
792
return json ;
788
793
}
789
794
@@ -819,9 +824,10 @@ JsonFree(Json *json)
819
824
Json *
820
825
JsonCopyTemporary (Json * tmp )
821
826
{
822
- Json * json = (Json * ) palloc (sizeof (Json ));
827
+ Size size = JsonAllocSize (tmp -> root .ops -> data_size );
828
+ Json * json = (Json * ) palloc (size );
823
829
824
- memcpy (json , tmp , sizeof ( Json ) );
830
+ memcpy (json , tmp , size );
825
831
tmp -> obj .freeValue = false;
826
832
tmp -> obj .isTemporary = false;
827
833
@@ -838,6 +844,7 @@ JsonValueToJson(JsonValue *val)
838
844
jc -> ops );
839
845
840
846
json -> root = * jc ;
847
+ memcpy (json -> root ._data , jc -> _data , jc -> ops -> data_size );
841
848
842
849
return json ;
843
850
}
@@ -855,11 +862,11 @@ JsonValueToJson(JsonValue *val)
855
862
JsonContainer *
856
863
JsonCopyFlat (JsonContainer * jc )
857
864
{
858
- JsonContainerData * res = JsonContainerAlloc ();
865
+ JsonContainerData * res = JsonContainerAlloc (jc -> ops );
859
866
860
867
* res = * jc ;
861
- res -> data = palloc (jc -> len );
862
- memcpy (res -> data , jc -> data , jc -> len );
868
+ JsonContainerDataPtr ( res ) = palloc (jc -> len );
869
+ memcpy (JsonContainerDataPtr ( res ), JsonContainerDataPtr ( jc ) , jc -> len );
863
870
864
871
return res ;
865
872
}
0 commit comments