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

#include <JSONPath.h>

Overview

A minimal JSONPath implementation.

Definition at line 42 of file JSONPath.h.

Inheritance diagram for JSONPath:
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_JSONPath (void)
 The JSONPath archetype. More...
 
ident objectForKeyPath (const ident root, const char *path)
 Access a nested property from JSON 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

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

Property Details

◆ interface

JSONPathInterface* JSONPath::interface
protected

The interface.

Definition at line 53 of file JSONPath.h.

◆ object

Object JSONPath::object

The superclass.

Definition at line 47 of file JSONPath.h.

Method Details

◆ _JSONPath()

Class * _JSONPath ( void  )

The JSONPath archetype.

Returns
The JSONPath Class.

Definition at line 118 of file JSONPath.c.

118 {
119 static Class *clazz;
120 static Once once;
121
122 do_once(&once, {
123 clazz = _initialize(&(const ClassDef) {
124 .name = "JSONPath",
125 .superclass = _Object(),
126 .instanceSize = sizeof(JSONPath),
127 .interfaceOffset = offsetof(JSONPath, interface),
128 .interfaceSize = sizeof(JSONPathInterface),
130 .destroy = destroy,
131 });
132 });
133
134 return clazz;
135}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void destroy(Class *clazz)
Definition: JSONPath.c:109
static void initialize(Class *clazz)
Definition: JSONPath.c:99
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
A minimal JSONPath implementation.
Definition: JSONPath.h:42
JSONPathInterface * interface
The interface.
Definition: JSONPath.h:53
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

◆ objectForKeyPath()

ident objectForKeyPath ( const ident  root,
const char *  path 
)

Access a nested property from JSON Data.

Parameters
rootThe root element.
pathThe JSONPath expression.
Returns
The nested property, or NULL if not found. Accessing a nested boolean from JSON Data: id obj = $$(JSONSerialization, objectFromData, data, 0); Boole *boole = $$(JSONPath, objectForKeyPath, obj, "$.foo.bar[1].baz"); Use dot-notation (.) for accessing Dictionaries, and square braces ([0]) for Arrays.

Definition at line 45 of file JSONPath.c.

45 {
46
47 assert(root);
48 assert(path);
49
50 assert(*path == '$');
51 const char *c = path;
52
53 ident obj = root;
54 while (obj) {
55
56 Range *matches;
57 if ($(_re, matchesCharacters, c, 0, &matches) == false) {
58 break;
59 }
60
61 const uint8_t *bytes = (uint8_t *) c + matches[1].location;
62 const size_t length = matches[1].length;
63
64 String *segment = $$(String, stringWithBytes, bytes, length, STRING_ENCODING_UTF8);
65 if (*segment->chars == '.') {
66
68 const Range range = { .location = 1, .length = segment->length - 1 };
69
70 String *key = $(segment, substring, range);
71
72 obj = $(dictionary, objectForKey, key);
73
74 release(key);
75
76 } else if (*segment->chars == '[') {
77
78 const Array *array = cast(Array, obj);
79 const unsigned long index = strtoul(segment->chars + 1, NULL, 10);
80 if (index < array->count) {
81 obj = $(array, objectAtIndex, index);
82 } else {
83 obj = NULL;
84 }
85 }
86 release(segment);
87
88 c += length;
89 }
90
91 return obj;
92}
static ident objectAtIndex(const Array *self, size_t index)
Definition: Array.c:394
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
#define obj
#define cast(type, obj)
Safely cast obj to type.
Definition: Class.h:165
static ident objectForKey(const Dictionary *self, const ident key)
Definition: Dictionary.c:382
static Regexp * _re
Definition: JSONPath.c:39
static MutableArray * array(void)
Definition: MutableArray.c:153
static MutableDictionary * dictionary(void)
static _Bool matchesCharacters(const Regexp *self, const char *chars, int options, Range **ranges)
Definition: Regexp.c:134
static String * substring(const String *self, const Range range)
Definition: String.c:539
static String * stringWithBytes(const uint8_t *bytes, size_t length, StringEncoding encoding)
Definition: String.c:478
@ STRING_ENCODING_UTF8
Definition: String.h:53
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
Immutable arrays.
Definition: Array.h:56
Immutable key-value stores.
Definition: Dictionary.h:60
A location and length into contiguous collections.
Definition: Types.h:54
ssize_t location
The location.
Definition: Types.h:59
size_t length
The length.
Definition: Types.h:64
Immutable UTF-8 strings.
Definition: String.h:69
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition: String.h:85
size_t length
The length of the String in bytes.
Definition: String.h:90

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