| 628 cx_strfree_a(json->allocator, &json->uncompleted_member.name); |
628 cx_strfree_a(json->allocator, &json->uncompleted_member.name); |
| 629 json->uncompleted_member = (CxJsonObjValue){{NULL, 0}, NULL}; |
629 json->uncompleted_member = (CxJsonObjValue){{NULL, 0}, NULL}; |
| 630 } |
630 } |
| 631 } |
631 } |
| 632 |
632 |
| |
633 void cxJsonReset(CxJson *json) { |
| |
634 const CxAllocator *allocator = json->allocator; |
| |
635 cxJsonDestroy(json); |
| |
636 cxJsonInit(json, allocator); |
| |
637 } |
| |
638 |
| 633 int cxJsonFilln(CxJson *json, const char *buf, size_t size) { |
639 int cxJsonFilln(CxJson *json, const char *buf, size_t size) { |
| 634 if (cxBufferEof(&json->buffer)) { |
640 if (cxBufferEof(&json->buffer)) { |
| 635 // reinitialize the buffer |
641 // reinitialize the buffer |
| 636 cxBufferDestroy(&json->buffer); |
642 cxBufferDestroy(&json->buffer); |
| 637 cxBufferInit(&json->buffer, (char*) buf, size, |
643 cxBufferInit(&json->buffer, (char*) buf, size, |
| 1124 } |
1130 } |
| 1125 value->value.array.array_size--; |
1131 value->value.array.array_size--; |
| 1126 return ret; |
1132 return ret; |
| 1127 } |
1133 } |
| 1128 |
1134 |
| |
1135 char *cxJsonAsString(const CxJsonValue *value) { |
| |
1136 return value->value.string.ptr; |
| |
1137 } |
| |
1138 |
| |
1139 cxstring cxJsonAsCxString(const CxJsonValue *value) { |
| |
1140 return cx_strcast(value->value.string); |
| |
1141 } |
| |
1142 |
| |
1143 cxmutstr cxJsonAsCxMutStr(const CxJsonValue *value) { |
| |
1144 return value->value.string; |
| |
1145 } |
| |
1146 |
| |
1147 double cxJsonAsDouble(const CxJsonValue *value) { |
| |
1148 if (value->type == CX_JSON_INTEGER) { |
| |
1149 return (double) value->value.integer; |
| |
1150 } else { |
| |
1151 return value->value.number; |
| |
1152 } |
| |
1153 } |
| |
1154 |
| |
1155 int64_t cxJsonAsInteger(const CxJsonValue *value) { |
| |
1156 if (value->type == CX_JSON_INTEGER) { |
| |
1157 return value->value.integer; |
| |
1158 } else { |
| |
1159 return (int64_t) value->value.number; |
| |
1160 } |
| |
1161 } |
| |
1162 |
| 1129 CxIterator cxJsonArrIter(const CxJsonValue *value) { |
1163 CxIterator cxJsonArrIter(const CxJsonValue *value) { |
| 1130 return cxIteratorPtr( |
1164 return cxIteratorPtr( |
| 1131 value->value.array.array, |
1165 value->value.array.array, |
| 1132 value->value.array.array_size |
1166 value->value.array.array_size, |
| |
1167 true // arrays need to keep order |
| 1133 ); |
1168 ); |
| 1134 } |
1169 } |
| 1135 |
1170 |
| 1136 CxIterator cxJsonObjIter(const CxJsonValue *value) { |
1171 CxIterator cxJsonObjIter(const CxJsonValue *value) { |
| 1137 return cxIterator( |
1172 return cxIterator( |
| 1138 value->value.object.values, |
1173 value->value.object.values, |
| 1139 sizeof(CxJsonObjValue), |
1174 sizeof(CxJsonObjValue), |
| 1140 value->value.object.values_size |
1175 value->value.object.values_size, |
| |
1176 true // TODO: objects do not always need to keep order |
| 1141 ); |
1177 ); |
| 1142 } |
1178 } |
| 1143 |
1179 |
| 1144 CxJsonValue *cx_json_obj_get(const CxJsonValue *value, cxstring name) { |
1180 CxJsonValue *cx_json_obj_get(const CxJsonValue *value, cxstring name) { |
| 1145 size_t index = json_find_objvalue(value, name); |
1181 size_t index = json_find_objvalue(value, name); |
| 1155 if (index >= value->value.object.values_size) { |
1191 if (index >= value->value.object.values_size) { |
| 1156 return NULL; |
1192 return NULL; |
| 1157 } else { |
1193 } else { |
| 1158 CxJsonObjValue kv = value->value.object.values[index]; |
1194 CxJsonObjValue kv = value->value.object.values[index]; |
| 1159 cx_strfree_a(value->allocator, &kv.name); |
1195 cx_strfree_a(value->allocator, &kv.name); |
| 1160 // TODO: replace with cx_array_remove() |
1196 // TODO: replace with cx_array_remove() / cx_array_remove_fast() |
| 1161 value->value.object.values_size--; |
1197 value->value.object.values_size--; |
| 1162 memmove(value->value.object.values + index, value->value.object.values + index + 1, (value->value.object.values_size - index) * sizeof(CxJsonObjValue)); |
1198 memmove(value->value.object.values + index, value->value.object.values + index + 1, (value->value.object.values_size - index) * sizeof(CxJsonObjValue)); |
| 1163 return kv.value; |
1199 return kv.value; |
| 1164 } |
1200 } |
| 1165 } |
1201 } |