Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes
JSONSerialization Struct Reference

#include <JSONSerialization.h>

Overview

JSON serialization and introspection.

Definition at line 63 of file JSONSerialization.h.

Inheritance diagram for JSONSerialization:
Object

Properties

Object object
 The superclass. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

Class_JSONSerialization (void)
 The JSONSerialization archetype. More...
 
DatadataFromObject (const ident obj, int options)
 Serializes the given Object to JSON Data. More...
 
ident objectFromData (const Data *data, int options)
 Parses an Object from the specified Data. More...
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype. More...
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object. More...
 
void dealloc (Object *self)
 Frees all resources held by this Object. More...
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object. More...
 
_Bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object. More...
 
_Bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership. More...
 

Protected Attributes

JSONSerializationInterface * interface
 The interface. More...
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface. More...
 

Property Details

◆ interface

JSONSerializationInterface* JSONSerialization::interface
protected

The interface.

Definition at line 74 of file JSONSerialization.h.

◆ object

Object JSONSerialization::object

The superclass.

Definition at line 68 of file JSONSerialization.h.

Method Details

◆ _JSONSerialization()

Class * _JSONSerialization ( void  )

The JSONSerialization archetype.

Returns
The JSONSerialization Class.

Definition at line 511 of file JSONSerialization.c.

511 {
512 static Class *clazz;
513 static Once once;
514
515 do_once(&once, {
516 clazz = _initialize(&(const ClassDef) {
517 .name = "JSONSerialization",
518 .superclass = _Object(),
519 .instanceSize = sizeof(JSONSerialization),
520 .interfaceOffset = offsetof(JSONSerialization, interface),
521 .interfaceSize = sizeof(JSONSerializationInterface),
523 });
524 });
525
526 return clazz;
527}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
long Once
The Once type.
Definition: Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition: Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition: Class.h:41
The runtime representation of a Class.
Definition: Class.h:95
JSON serialization and introspection.
JSONSerializationInterface * interface
The interface.
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51
Class * _Object(void)
The Object archetype.
Definition: Object.c:136

◆ dataFromObject()

Data * dataFromObject ( const ident  obj,
int  options 
)

Serializes the given Object to JSON Data.

Parameters
objThe Object to serialize.
optionsA bitwise-or of JSON_WRITE_*.
Returns
The resulting JSON Data.

Definition at line 223 of file JSONSerialization.c.

223 {
224
225 if (obj) {
226 JSONWriter writer = {
227 .data = $(alloc(MutableData), init),
228 .options = options
229 };
230
231 writeObject(&writer, obj);
232
233 return (Data *) writer.data;
234 }
235
236 return NULL;
237}
#define obj
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
static void writeObject(JSONWriter *writer, const Dictionary *object)
Writes object to writer.
Immutable data buffers.
Definition: Data.h:50
A writer for generating JSON Data.
MutableData * data
Mutable data buffers.
Definition: MutableData.h:40
Object * init(Object *self)
Initializes this Object.
Definition: Object.c:83

◆ objectFromData()

ident objectFromData ( const Data data,
int  options 
)

Parses an Object from the specified Data.

Parameters
dataThe JSON Data.
optionsA bitwise-or of JSON_READ_*.
Returns
The Object, or NULL on error.

Definition at line 482 of file JSONSerialization.c.

482 {
483
484 if (data && data->length) {
485 JSONReader reader = {
486 .data = data,
487 .options = options
488 };
489
490 return readElement(&reader);
491 }
492
493 return NULL;
494}
static ident readElement(JSONReader *reader)
Reads an element from reader. An element is any valid JSON type.
static MutableData * data(void)
Definition: MutableData.c:75
size_t length
The length of bytes.
Definition: Data.h:76
A reader for parsing JSON Data.
const Data * data

The documentation for this struct was generated from the following files: