@@ -128,7 +128,7 @@ JsonValueUnwrap(const JsonValue *val, JsonValue *valbuf)
128128
129129 if (jc -> ops == & jsonvContainerOps )
130130 {
131- val = (JsonbValue * ) jc -> data ;
131+ val = (JsonbValue * ) JsonContainerDataPtr ( jc ) ;
132132 Assert (val -> type != jbvBinary );
133133 }
134134 else if (JsonContainerIsScalar (jc ))
@@ -304,7 +304,7 @@ static void
304304jsonvInitContainer (JsonContainerData * jc , const JsonValue * val )
305305{
306306 jc -> ops = & jsonvContainerOps ;
307- jc -> data = (void * ) val ;
307+ JsonContainerDataPtr ( jc ) = (void * ) val ;
308308 jc -> len = 0 ;
309309 jc -> size = val -> type == jbvBinary ? val -> val .binary .data -> size :
310310 val -> type == jbvObject ? val -> val .object .nPairs :
@@ -322,7 +322,7 @@ JsonValueToContainer(const JsonValue *val)
322322 return val -> val .binary .data ;
323323 else
324324 {
325- JsonContainerData * jc = JsonContainerAlloc ();
325+ JsonContainerData * jc = JsonContainerAlloc (& jsonvContainerOps );
326326 jsonvInitContainer (jc , val );
327327 return jc ;
328328 }
@@ -354,7 +354,7 @@ static JsonIteratorToken
354354jsonvScalarIteratorNext (JsonIterator * * it , JsonValue * res , bool skipNested )
355355{
356356 JsonvScalarIterator * sit = (JsonvScalarIterator * ) * it ;
357- JsonValue * val = ( * it )-> container -> data ;
357+ JsonValue * val = JsonContainerDataPtr (( * it )-> container ) ;
358358
359359 Assert (IsAJsonbScalar (val ));
360360
@@ -384,7 +384,7 @@ static JsonIteratorToken
384384jsonvArrayIteratorNext (JsonIterator * * it , JsonValue * res , bool skipNested )
385385{
386386 JsonvArrayIterator * ait = (JsonvArrayIterator * ) * it ;
387- JsonValue * arr = ( * it )-> container -> data ;
387+ JsonValue * arr = JsonContainerDataPtr (( * it )-> container ) ;
388388 JsonValue * val ;
389389
390390 Assert (arr -> type == jbvArray );
@@ -428,7 +428,7 @@ static JsonIteratorToken
428428jsonvObjectIteratorNext (JsonIterator * * it , JsonValue * res , bool skipNested )
429429{
430430 JsonvObjectIterator * oit = (JsonvObjectIterator * ) * it ;
431- JsonValue * obj = ( * it )-> container -> data ;
431+ JsonValue * obj = JsonContainerDataPtr (( * it )-> container ) ;
432432 JsonPair * pair ;
433433
434434 Assert (obj -> type == jbvObject );
@@ -553,13 +553,13 @@ jsonvIteratorInitFromValue(JsonValue *val, JsonContainer *jsc)
553553static JsonIterator *
554554jsonvIteratorInit (JsonContainer * jsc )
555555{
556- return jsonvIteratorInitFromValue (jsc -> data , jsc );
556+ return jsonvIteratorInitFromValue (JsonContainerDataPtr ( jsc ) , jsc );
557557}
558558
559559static JsonValue *
560560jsonvFindKeyInObject (JsonContainer * objc , const char * key , int len )
561561{
562- JsonValue * obj = ( JsonValue * ) objc -> data ;
562+ JsonValue * obj = JsonContainerDataPtr ( objc ) ;
563563 JsonValue * res ;
564564 JsonValue * jv ;
565565 int i ;
@@ -605,7 +605,7 @@ jsonvFindKeyInObject(JsonContainer *objc, const char *key, int len)
605605static JsonValue *
606606jsonvFindValueInArray (JsonContainer * arrc , const JsonValue * val )
607607{
608- JsonValue * arr = ( JsonValue * ) arrc -> data ;
608+ JsonValue * arr = JsonContainerDataPtr ( arrc ) ;
609609
610610 Assert (JsonContainerIsArray (arrc ));
611611 Assert (IsAJsonbScalar (val ));
@@ -640,7 +640,7 @@ jsonvFindValueInArray(JsonContainer *arrc, const JsonValue *val)
640640static JsonValue *
641641jsonvGetArrayElement (JsonContainer * arrc , uint32 index )
642642{
643- JsonValue * arr = ( JsonValue * ) arrc -> data ;
643+ JsonValue * arr = JsonContainerDataPtr ( arrc ) ;
644644
645645 Assert (JsonContainerIsArray (arrc ));
646646
@@ -668,7 +668,7 @@ jsonvGetArrayElement(JsonContainer *arrc, uint32 index)
668668static uint32
669669jsonvGetArraySize (JsonContainer * arrc )
670670{
671- JsonValue * arr = ( JsonValue * ) arrc -> data ;
671+ JsonValue * arr = JsonContainerDataPtr ( arrc ) ;
672672
673673 Assert (JsonContainerIsArray (arrc ));
674674
@@ -692,17 +692,18 @@ jsonvGetArraySize(JsonContainer *arrc)
692692static JsonContainer *
693693jsonvCopy (JsonContainer * jc )
694694{
695- JsonContainerData * res = JsonContainerAlloc ();
695+ JsonContainerData * res = JsonContainerAlloc (& jsonvContainerOps );
696696
697697 * res = * jc ;
698- res -> data = JsonValueCopy (NULL , ( JsonValue * ) jc -> data );
698+ JsonContainerDataPtr ( res ) = JsonValueCopy (NULL , JsonContainerDataPtr ( jc ) );
699699
700700 return res ;
701701}
702702
703703JsonContainerOps
704704jsonvContainerOps =
705705{
706+ sizeof (JsonValue * ),
706707 NULL ,
707708 jsonvIteratorInit ,
708709 jsonvFindKeyInObject ,
@@ -717,7 +718,7 @@ JsonValue *
717718JsonToJsonValue (Json * json , JsonValue * jv )
718719{
719720 if (JsonRoot (json )-> ops == & jsonvContainerOps )
720- return ( JsonValue * ) JsonRoot (json )-> data ;
721+ return JsonContainerDataPtr ( JsonRoot (json )) ;
721722
722723 if (!jv )
723724 jv = palloc (sizeof (JsonValue ));
@@ -731,9 +732,9 @@ JsonInit(Json *json)
731732 const void * data = DatumGetPointer (json -> obj .value );
732733 const void * detoasted_data ;
733734
734- Assert (json -> root . data || data );
735+ Assert (JsonContainerDataPtr ( & json -> root ) || data );
735736
736- if (json -> root . data || !data )
737+ if (JsonContainerDataPtr ( & json -> root ) || !data ) /* FIXME */
737738 return ;
738739
739740 detoasted_data = PG_DETOAST_DATUM (json -> obj .value );
@@ -750,13 +751,16 @@ JsonExpand(Json *tmp, Datum value, bool freeValue, JsonContainerOps *ops)
750751
751752 if (tmp )
752753 {
754+ Assert (0 );
753755 json = tmp ;
754756 json -> obj .isTemporary = true;
755757 }
756758 else
757759 {
760+ Size size = JsonAllocSize (ops -> data_size );
761+
758762#ifndef JSON_EXPANDED_OBJECT_MCXT
759- json = (Json * ) palloc (sizeof ( Json ) );
763+ json = (Json * ) palloc (size );
760764#else
761765 /*
762766 * Allocate private context for expanded object. We start by assuming
@@ -770,20 +774,21 @@ JsonExpand(Json *tmp, Datum value, bool freeValue, JsonContainerOps *ops)
770774 ALLOCSET_SMALL_INITSIZE ,
771775 ALLOCSET_DEFAULT_MAXSIZE );
772776
773- json = (Json * ) MemoryContextAlloc (objcxt , sizeof ( Json ) );
777+ json = (Json * ) MemoryContextAlloc (objcxt , size );
774778#endif
775779 json -> obj .isTemporary = false;
776780 }
777781
778782 json -> obj .value = value ;
779783 json -> obj .freeValue = freeValue ;
780- json -> root .data = NULL ;
781784 json -> root .len = 0 ;
782785 json -> root .ops = ops ;
783786 json -> root .size = -1 ;
784787 json -> root .type = jbvBinary ;
785788 json -> is_json = false;
786789
790+ memset (json -> root ._data , 0 , ops -> data_size );
791+
787792 return json ;
788793}
789794
@@ -819,9 +824,10 @@ JsonFree(Json *json)
819824Json *
820825JsonCopyTemporary (Json * tmp )
821826{
822- Json * json = (Json * ) palloc (sizeof (Json ));
827+ Size size = JsonAllocSize (tmp -> root .ops -> data_size );
828+ Json * json = (Json * ) palloc (size );
823829
824- memcpy (json , tmp , sizeof ( Json ) );
830+ memcpy (json , tmp , size );
825831 tmp -> obj .freeValue = false;
826832 tmp -> obj .isTemporary = false;
827833
@@ -838,6 +844,7 @@ JsonValueToJson(JsonValue *val)
838844 jc -> ops );
839845
840846 json -> root = * jc ;
847+ memcpy (json -> root ._data , jc -> _data , jc -> ops -> data_size );
841848
842849 return json ;
843850 }
@@ -855,11 +862,11 @@ JsonValueToJson(JsonValue *val)
855862JsonContainer *
856863JsonCopyFlat (JsonContainer * jc )
857864{
858- JsonContainerData * res = JsonContainerAlloc ();
865+ JsonContainerData * res = JsonContainerAlloc (jc -> ops );
859866
860867 * 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 );
863870
864871 return res ;
865872}
0 commit comments