- Timestamp:
- 2015-05-05T22:39:39Z (10 years ago)
- Children:
- f81d8b8
- Parents:
- 5726a0d
- Location:
- lib
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/parson.c
r5726a0d r8563fe7 27 27 #include "parson.h" 28 28 29 #include <errno.h> 29 30 #include <stdio.h> 30 31 #include <stdlib.h> … … 38 39 #define MAX_NESTING 19 39 40 #define DOUBLE_SERIALIZATION_FORMAT "%f" 41 #define JINT_SERIALIZATION_FORMAT "%lld" 40 42 41 43 #define SIZEOF_TOKEN(a) (sizeof(a) - 1) … … 53 55 #define IS_CONT(b) (((unsigned char)(b) & 0xC0) == 0x80) /* is utf-8 continuation byte */ 54 56 57 typedef long long jint; 58 55 59 /* Type definitions */ 56 60 typedef union json_value_value { 57 61 const char *string; 58 62 double number; 63 jint integer; 59 64 JSON_Object *object; 60 65 JSON_Array *array; … … 670 675 671 676 static JSON_Value * parse_number_value(const char **string) { 672 char *end; 673 double number = strtod(*string, &end); 677 char *jint_end, *double_end; 678 double number = strtod(*string, &double_end); 679 jint integer = strtoll(*string, &jint_end, 10); 674 680 JSON_Value *output_value; 675 if (is_decimal(*string, end - *string)) { 676 *string = end; 677 output_value = json_value_init_number(number); 681 if (double_end > jint_end || errno == ERANGE) { 682 if (is_decimal(*string, double_end - *string)) { 683 *string = double_end; 684 output_value = json_value_init_number(number); 685 } else { 686 output_value = NULL; 687 } 678 688 } else { 679 output_value = NULL; 689 *string = jint_end; 690 output_value = json_value_init_integer(integer); 680 691 } 681 692 return output_value; … … 700 711 size_t i = 0, count = 0; 701 712 double num = 0.0; 713 jint intnum = 0; 702 714 switch (json_value_get_type(value)) { 703 715 case JSONArray: … … 731 743 else 732 744 return 5; /* strlen("false"); */ 745 case JSONInteger: 746 intnum = json_value_get_integer(value); 747 return (size_t)sprintf(buf, JINT_SERIALIZATION_FORMAT, intnum); 733 748 case JSONNumber: 734 749 num = json_value_get_number(value); 735 if (num == ((double)( int)num) ) /* check if num is integer */736 return (size_t)sprintf(buf, "%d", (int)num);750 if (num == ((double)(jint)num) ) /* check if num is integer */ 751 return (size_t)sprintf(buf, JINT_SERIALIZATION_FORMAT, (jint)num); 737 752 return (size_t)sprintf(buf, DOUBLE_SERIALIZATION_FORMAT, num); 738 753 case JSONNull: … … 753 768 size_t i = 0, count = 0; 754 769 double num = 0.0; 770 jint intnum = 0; 755 771 switch (json_value_get_type(value)) { 756 772 case JSONArray: … … 798 814 } 799 815 return buf; 816 case JSONInteger: 817 intnum = json_value_get_integer(value); 818 PRINTF_AND_SKIP(buf, JINT_SERIALIZATION_FORMAT, intnum); 819 return buf; 800 820 case JSONNumber: 801 821 num = json_value_get_number(value); … … 912 932 } 913 933 934 jint json_object_get_integer(const JSON_Object *object, const char *name) { 935 return json_value_get_integer(json_object_get_value(object, name)); 936 } 937 914 938 double json_object_get_number(const JSON_Object *object, const char *name) { 915 939 return json_value_get_number(json_object_get_value(object, name)); … … 938 962 const char * json_object_dotget_string(const JSON_Object *object, const char *name) { 939 963 return json_value_get_string(json_object_dotget_value(object, name)); 964 } 965 966 jint json_object_dotget_integer(const JSON_Object *object, const char *name) { 967 return json_value_get_integer(json_object_dotget_value(object, name)); 940 968 } 941 969 … … 986 1014 } 987 1015 1016 jint json_array_get_integer(const JSON_Array *array, size_t index) { 1017 return json_value_get_integer(json_array_get_value(array, index)); 1018 } 1019 988 1020 double json_array_get_number(const JSON_Array *array, size_t index) { 989 1021 return json_value_get_number(json_array_get_value(array, index)); … … 1023 1055 } 1024 1056 1057 jint json_value_get_integer(const JSON_Value *value) { 1058 if (json_value_get_type(value) == JSONNumber) { 1059 return value->value.number; 1060 } else if (json_value_get_type(value) == JSONInteger) { 1061 return value->value.integer; 1062 } else { 1063 return 0; 1064 } 1065 } 1066 1025 1067 double json_value_get_number(const JSON_Value *value) { 1026 return json_value_get_type(value) == JSONNumber ? value->value.number : 0; 1068 if (json_value_get_type(value) == JSONNumber) { 1069 return value->value.number; 1070 } else if (json_value_get_type(value) == JSONInteger) { 1071 return value->value.integer; 1072 } else { 1073 return 0; 1074 } 1027 1075 } 1028 1076 … … 1090 1138 PARSON_FREE(copy); 1091 1139 return value; 1140 } 1141 1142 JSON_Value * json_value_init_integer(jint number) { 1143 JSON_Value *new_value = (JSON_Value*)PARSON_MALLOC(sizeof(JSON_Value)); 1144 if (!new_value) 1145 return NULL; 1146 new_value->type = JSONInteger; 1147 new_value->value.integer = number; 1148 return new_value; 1092 1149 } 1093 1150 … … 1277 1334 } 1278 1335 1336 JSON_Status json_array_replace_integer(JSON_Array *array, size_t i, jint integer) { 1337 JSON_Value *value = json_value_init_integer(integer); 1338 if (value == NULL) 1339 return JSONFailure; 1340 if (json_array_replace_value(array, i, value) == JSONFailure) { 1341 json_value_free(value); 1342 return JSONFailure; 1343 } 1344 return JSONSuccess; 1345 } 1346 1279 1347 JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number) { 1280 1348 JSON_Value *value = json_value_init_number(number); … … 1329 1397 JSON_Status json_array_append_string(JSON_Array *array, const char *string) { 1330 1398 JSON_Value *value = json_value_init_string(string); 1399 if (value == NULL) 1400 return JSONFailure; 1401 if (json_array_append_value(array, value) == JSONFailure) { 1402 json_value_free(value); 1403 return JSONFailure; 1404 } 1405 return JSONSuccess; 1406 } 1407 1408 JSON_Status json_array_append_integer(JSON_Array *array, jint integer) { 1409 JSON_Value *value = json_value_init_integer(integer); 1331 1410 if (value == NULL) 1332 1411 return JSONFailure; … … 1392 1471 JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string) { 1393 1472 return json_object_set_value(object, name, json_value_init_string(string)); 1473 } 1474 1475 JSON_Status json_object_set_integer(JSON_Object *object, const char *name, jint integer) { 1476 return json_object_set_value(object, name, json_value_init_integer(integer)); 1394 1477 } 1395 1478 … … 1439 1522 JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string) { 1440 1523 JSON_Value *value = json_value_init_string(string); 1524 if (value == NULL) 1525 return JSONFailure; 1526 if (json_object_dotset_value(object, name, value) == JSONFailure) { 1527 json_value_free(value); 1528 return JSONFailure; 1529 } 1530 return JSONSuccess; 1531 } 1532 1533 JSON_Status json_object_dotset_integer(JSON_Object *object, const char *name, jint integer) { 1534 JSON_Value *value = json_value_init_integer(integer); 1441 1535 if (value == NULL) 1442 1536 return JSONFailure; … … 1663 1757 } 1664 1758 1759 jint json_integer (const JSON_Value *value) { 1760 return json_value_get_integer(value); 1761 } 1762 1665 1763 double json_number (const JSON_Value *value) { 1666 1764 return json_value_get_number(value); -
lib/parson.h
r5726a0d r8563fe7 37 37 typedef struct json_value_t JSON_Value; 38 38 39 typedef long long jint; 40 39 41 enum json_value_type { 40 42 JSONError = -1, 41 43 JSONNull = 1, 42 44 JSONString = 2, 45 JSONInteger = 7, 43 46 JSONNumber = 3, 44 47 JSONObject = 4, … … 100 103 JSON_Object * json_object_get_object (const JSON_Object *object, const char *name); 101 104 JSON_Array * json_object_get_array (const JSON_Object *object, const char *name); 105 jint json_object_get_integer(const JSON_Object *object, const char *name); /* returns 0 on fail */ 102 106 double json_object_get_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ 103 107 int json_object_get_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ … … 111 115 JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name); 112 116 JSON_Array * json_object_dotget_array (const JSON_Object *object, const char *name); 117 jint json_object_dotget_integer(const JSON_Object *object, const char *name); /* returns 0 on fail */ 113 118 double json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ 114 119 int json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ … … 123 128 JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value); 124 129 JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string); 130 JSON_Status json_object_set_integer(JSON_Object *object, const char *name, jint number); 125 131 JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number); 126 132 JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean); … … 130 136 JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value); 131 137 JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string); 138 JSON_Status json_object_dotset_integer(JSON_Object *object, const char *name, jint number); 132 139 JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number); 133 140 JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean); … … 150 157 JSON_Object * json_array_get_object (const JSON_Array *array, size_t index); 151 158 JSON_Array * json_array_get_array (const JSON_Array *array, size_t index); 159 jint json_array_get_integer(const JSON_Array *array, size_t index); /* returns 0 on fail */ 152 160 double json_array_get_number (const JSON_Array *array, size_t index); /* returns 0 on fail */ 153 161 int json_array_get_boolean(const JSON_Array *array, size_t index); /* returns -1 on fail */ … … 162 170 JSON_Status json_array_replace_value(JSON_Array *array, size_t i, JSON_Value *value); 163 171 JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string); 172 JSON_Status json_array_replace_integer(JSON_Array *array, size_t i, jint number); 164 173 JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number); 165 174 JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean); … … 172 181 JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value); 173 182 JSON_Status json_array_append_string(JSON_Array *array, const char *string); 183 JSON_Status json_array_append_integer(JSON_Array *array, jint number); 174 184 JSON_Status json_array_append_number(JSON_Array *array, double number); 175 185 JSON_Status json_array_append_boolean(JSON_Array *array, int boolean); … … 182 192 JSON_Value * json_value_init_array (void); 183 193 JSON_Value * json_value_init_string (const char *string); /* copies passed string */ 194 JSON_Value * json_value_init_integer(jint number); 184 195 JSON_Value * json_value_init_number (double number); 185 196 JSON_Value * json_value_init_boolean(int boolean); … … 192 203 JSON_Array * json_value_get_array (const JSON_Value *value); 193 204 const char * json_value_get_string (const JSON_Value *value); 205 jint json_value_get_integer(const JSON_Value *value); 194 206 double json_value_get_number (const JSON_Value *value); 195 207 int json_value_get_boolean(const JSON_Value *value); … … 200 212 JSON_Array * json_array (const JSON_Value *value); 201 213 const char * json_string (const JSON_Value *value); 214 jint json_integer(const JSON_Value *value); 202 215 double json_number (const JSON_Value *value); 203 216 int json_boolean(const JSON_Value *value);
Note: See TracChangeset
for help on using the changeset viewer.