3939
4040#define _Class _JSONContext
4141
42- #pragma mark - Error accumulation
42+ #pragma mark - Object
43+
44+ /**
45+ * @see Object::dealloc(Object *)
46+ */
47+ static void dealloc (Object * self ) {
48+
49+ JSONContext * this = (JSONContext * ) self ;
50+
51+ release (this -> errors );
52+
53+ super (Object , self , dealloc );
54+ }
55+
56+ #pragma mark - Writer
4357
4458/**
4559 * @brief Records an error on the context.
@@ -74,8 +88,6 @@ static void addError(JSONContext *self, int code, const char *key, const char *d
7488 release (error );
7589}
7690
77- #pragma mark - Writer
78-
7991/**
8092 * @brief Internal state for JSON text generation.
8193 */
@@ -563,100 +575,7 @@ static ident readElement(JSONReader *reader) {
563575 return NULL ;
564576}
565577
566- #pragma mark - Internal helpers
567-
568- /**
569- * @brief Serializes a C struct instance to a Dictionary.
570- */
571- static Dictionary * dictionaryFromStruct (JSONContext * self ,
572- const JSONProperties * properties ,
573- const ident instance ) {
574-
575- if (!properties || !instance ) {
576- return NULL ;
577- }
578-
579- Dictionary * dict = $ (alloc (Dictionary ), init );
580-
581- for (const JSONProperty * p = properties -> properties ; p -> key ; p ++ ) {
582- if (!p -> serializer ) {
583- continue ;
584- }
585-
586- const ident field = instance + p -> offset ;
587- ident val = p -> serializer (properties , p , field , p -> data , self );
588-
589- if (val ) {
590- String * key = $$ (String , stringWithCharacters , p -> key );
591- $ (dict , setObjectForKey , val , key );
592- release (key );
593- release (val );
594- }
595- }
596-
597- return (Dictionary * ) dict ;
598- }
599-
600- /**
601- * @brief Deserializes a Dictionary into a C struct.
602- */
603- static bool structFromDictionary (JSONContext * self ,
604- const JSONProperties * properties ,
605- const Dictionary * dictionary ,
606- ident instance ) {
607-
608- if (!properties || !dictionary || !instance ) {
609- return false;
610- }
611-
612- bool ok = true;
613-
614- for (const JSONProperty * p = properties -> properties ; p -> key ; p ++ ) {
615- if (!p -> deserializer ) {
616- continue ;
617- }
618-
619- String * key = $$ (String , stringWithCharacters , p -> key );
620- const Object * val = $ (dictionary , objectForKey , key );
621- release (key );
622-
623- if (!val ) {
624- continue ;
625- }
626-
627- if (!p -> deserializer (properties , p , val , instance + p -> offset , self )) {
628- addError (self , 2 , p -> key , "type mismatch" );
629- ok = false;
630- }
631- }
632-
633- return ok ;
634- }
635-
636- /**
637- * @brief Deserializes a JSON Array into an array of C structs.
638- */
639- static size_t structsFromArray (JSONContext * self ,
640- const JSONProperties * properties ,
641- const Array * array ,
642- ident instances ,
643- size_t count ) {
644-
645- if (!properties || !array || !instances || !count ) {
646- return 0 ;
647- }
648-
649- const size_t n = min (array -> count , count );
650-
651- for (size_t i = 0 ; i < n ; i ++ ) {
652- const Dictionary * dictionary = cast (Dictionary , $ (array , objectAtIndex , i ));
653- structFromDictionary (self , properties , dictionary , instances + i * properties -> size );
654- }
655-
656- return n ;
657- }
658-
659- #pragma mark - JSONContext instance methods
578+ #pragma mark - JSONContext
660579
661580/**
662581 * @fn Data *JSONContext::dataFromObject(JSONContext *self, const ident obj, int options)
@@ -684,11 +603,9 @@ static Data *dataFromObject(JSONContext *self, const ident obj, int options) {
684603 * @fn Data *JSONContext::dataFromStruct(JSONContext *self, const JSONProperties *properties, const ident instance)
685604 * @memberof JSONContext
686605 */
687- static Data * dataFromStruct (JSONContext * self ,
688- const JSONProperties * properties ,
689- const ident instance ) {
606+ static Data * dataFromStruct (JSONContext * self , const JSONProperties * properties , const ident instance ) {
690607
691- Dictionary * dict = dictionaryFromStruct (self , properties , instance );
608+ Dictionary * dict = $ (self , dictionaryFromStruct , properties , instance );
692609 Data * data = dataFromObject (self , dict , 0 );
693610 release (dict );
694611 return data ;
@@ -698,10 +615,7 @@ static Data *dataFromStruct(JSONContext *self,
698615 * @fn Data *JSONContext::dataFromStructs(JSONContext *self, const JSONProperties *properties, const ident instances, size_t count)
699616 * @memberof JSONContext
700617 */
701- static Data * dataFromStructs (JSONContext * self ,
702- const JSONProperties * properties ,
703- const ident instances ,
704- size_t count ) {
618+ static Data * dataFromStructs (JSONContext * self , const JSONProperties * properties , const ident instances , size_t count ) {
705619
706620 if (!count ) {
707621 return NULL ;
@@ -711,7 +625,7 @@ static Data *dataFromStructs(JSONContext *self,
711625
712626 for (size_t i = 0 ; i < count ; i ++ ) {
713627 const ident instance = instances + i * properties -> size ;
714- Dictionary * dict = dictionaryFromStruct (self , properties , instance );
628+ Dictionary * dict = $ (self , dictionaryFromStruct , properties , instance );
715629 $ (array , addObject , dict );
716630 release (dict );
717631 }
@@ -721,6 +635,43 @@ static Data *dataFromStructs(JSONContext *self,
721635 return data ;
722636}
723637
638+ /**
639+ * @brief Serializes a C struct instance to a Dictionary.
640+ */
641+ static Dictionary * dictionaryFromStruct (JSONContext * self , const JSONProperties * properties , const ident instance ) {
642+
643+ if (!properties || !instance ) {
644+ return NULL ;
645+ }
646+
647+ Dictionary * dict = $ (alloc (Dictionary ), init );
648+
649+ for (const JSONProperty * p = properties -> properties ; p -> key ; p ++ ) {
650+ if (!p -> serializer ) {
651+ continue ;
652+ }
653+
654+ const ident field = instance + p -> offset ;
655+ ident val = p -> serializer (properties , p , field , p -> data , self );
656+
657+ if (val ) {
658+ String * key = $$ (String , stringWithCharacters , p -> key );
659+ $ (dict , setObjectForKey , val , key );
660+ release (key );
661+ release (val );
662+ }
663+ }
664+
665+ return (Dictionary * ) dict ;
666+ }
667+
668+ /**
669+ * @see Object::init(Object *)
670+ */
671+ static JSONContext * init (JSONContext * self ) {
672+ return (JSONContext * ) super (Object , self , init );
673+ }
674+
724675/**
725676 * @fn ident JSONContext::objectFromData(JSONContext *self, const Data *data, int options)
726677 * @memberof JSONContext
@@ -747,16 +698,65 @@ static ident objectFromData(JSONContext *self, const Data *data, int options) {
747698 return NULL ;
748699}
749700
701+ /**
702+ * @brief Deserializes a Dictionary into a C struct.
703+ */
704+ static bool structFromDictionary (JSONContext * self , const JSONProperties * properties , const Dictionary * dictionary , ident instance ) {
705+
706+ if (!properties || !dictionary || !instance ) {
707+ return false;
708+ }
709+
710+ bool ok = true;
711+
712+ for (const JSONProperty * p = properties -> properties ; p -> key ; p ++ ) {
713+ if (!p -> deserializer ) {
714+ continue ;
715+ }
716+
717+ String * key = $$ (String , stringWithCharacters , p -> key );
718+ const Object * val = $ (dictionary , objectForKey , key );
719+ release (key );
720+
721+ if (!val ) {
722+ continue ;
723+ }
724+
725+ if (!p -> deserializer (properties , p , val , instance + p -> offset , self )) {
726+ addError (self , 2 , p -> key , "type mismatch" );
727+ ok = false;
728+ }
729+ }
730+
731+ return ok ;
732+ }
733+
734+ /**
735+ * @brief Deserializes a JSON Array into an array of C structs.
736+ */
737+ static size_t structsFromArray (JSONContext * self , const JSONProperties * properties , const Array * array , ident instances , size_t count ) {
738+
739+ if (!properties || !array || !instances || !count ) {
740+ return 0 ;
741+ }
742+
743+ const size_t n = min (array -> count , count );
744+
745+ for (size_t i = 0 ; i < n ; i ++ ) {
746+ const Dictionary * dictionary = cast (Dictionary , $ (array , objectAtIndex , i ));
747+ $ (self , structFromDictionary , properties , dictionary , instances + i * properties -> size );
748+ }
749+
750+ return n ;
751+ }
752+
750753/**
751754 * @fn bool JSONContext::structFromData(JSONContext *self, const JSONProperties *properties, const Data *data, ident instance)
752755 * @memberof JSONContext
753756 */
754- static bool structFromData (JSONContext * self ,
755- const JSONProperties * properties ,
756- const Data * data ,
757- ident instance ) {
757+ static bool structFromData (JSONContext * self , const JSONProperties * properties , const Data * data , ident instance ) {
758758
759- ident obj = objectFromData (self , data , 0 );
759+ ident obj = $ (self , objectFromData , data , 0 );
760760 if (!obj || !$ ((Object * ) obj , isKindOfClass , _Dictionary ())) {
761761 release (obj );
762762 return false;
@@ -772,11 +772,7 @@ static bool structFromData(JSONContext *self,
772772 * @fn size_t JSONContext::structsFromData(JSONContext *self, const JSONProperties *properties, const Data *data, ident instances, size_t count)
773773 * @memberof JSONContext
774774 */
775- static size_t structsFromData (JSONContext * self ,
776- const JSONProperties * properties ,
777- const Data * data ,
778- ident instances ,
779- size_t count ) {
775+ static size_t structsFromData (JSONContext * self , const JSONProperties * properties , const Data * data , ident instances , size_t count ) {
780776
781777 ident obj = objectFromData (self , data , 0 );
782778 if (!obj || !$ ((Object * ) obj , isKindOfClass , _Array ())) {
@@ -790,28 +786,6 @@ static size_t structsFromData(JSONContext *self,
790786 return n ;
791787}
792788
793- #pragma mark - Object lifecycle
794-
795- /**
796- * @see Object::dealloc(Object *)
797- */
798- static void dealloc (Object * self ) {
799-
800- JSONContext * this = (JSONContext * ) self ;
801-
802- release (this -> errors );
803-
804- super (Object , self , dealloc );
805- }
806-
807- /**
808- * @see Object::init(Object *)
809- */
810- static JSONContext * init (JSONContext * self ) {
811-
812- return (JSONContext * ) super (Object , self , init );
813- }
814-
815789#pragma mark - Class lifecycle
816790
817791/**
@@ -828,19 +802,16 @@ static void initialize(Class *clazz) {
828802
829803 ((ObjectInterface * ) clazz -> interface )-> dealloc = dealloc ;
830804
831- JSONContextInterface * iface = (JSONContextInterface * ) clazz -> interface ;
832-
833- iface -> init = init ;
834-
835- iface -> objectFromData = objectFromData ;
836- iface -> dataFromObject = dataFromObject ;
837- iface -> dataFromStruct = dataFromStruct ;
838- iface -> dataFromStructs = dataFromStructs ;
839- iface -> structFromData = structFromData ;
840- iface -> structsFromData = structsFromData ;
841- iface -> dictionaryFromStruct = dictionaryFromStruct ;
842- iface -> structFromDictionary = structFromDictionary ;
843- iface -> structsFromArray = structsFromArray ;
805+ ((JSONContextInterface * ) clazz -> interface )-> dataFromObject = dataFromObject ;
806+ ((JSONContextInterface * ) clazz -> interface )-> dataFromStruct = dataFromStruct ;
807+ ((JSONContextInterface * ) clazz -> interface )-> dataFromStructs = dataFromStructs ;
808+ ((JSONContextInterface * ) clazz -> interface )-> dictionaryFromStruct = dictionaryFromStruct ;
809+ ((JSONContextInterface * ) clazz -> interface )-> init = init ;
810+ ((JSONContextInterface * ) clazz -> interface )-> objectFromData = objectFromData ;
811+ ((JSONContextInterface * ) clazz -> interface )-> structFromData = structFromData ;
812+ ((JSONContextInterface * ) clazz -> interface )-> structsFromData = structsFromData ;
813+ ((JSONContextInterface * ) clazz -> interface )-> structFromDictionary = structFromDictionary ;
814+ ((JSONContextInterface * ) clazz -> interface )-> structsFromArray = structsFromArray ;
844815}
845816
846817/**
0 commit comments