@@ -242,6 +242,27 @@ namespace json
242
242
// / <returns>A JSON number value</returns>
243
243
static _ASYNCRTIMP value __cdecl number (int32_t value);
244
244
245
+ // / <summary>
246
+ // / Creates a number value
247
+ // / </summary>
248
+ // / <param name="value">The C++ value to create a JSON value from</param>
249
+ // / <returns>A JSON number value</returns>
250
+ static _ASYNCRTIMP value __cdecl number (uint32_t value);
251
+
252
+ // / <summary>
253
+ // / Creates a number value
254
+ // / </summary>
255
+ // / <param name="value">The C++ value to create a JSON value from</param>
256
+ // / <returns>A JSON number value</returns>
257
+ static _ASYNCRTIMP value __cdecl number (int64_t value);
258
+
259
+ // / <summary>
260
+ // / Creates a number value
261
+ // / </summary>
262
+ // / <param name="value">The C++ value to create a JSON value from</param>
263
+ // / <returns>A JSON number value</returns>
264
+ static _ASYNCRTIMP value __cdecl number (uint64_t value);
265
+
245
266
// / <summary>
246
267
// / Creates a Boolean value
247
268
// / </summary>
@@ -543,6 +564,18 @@ namespace json
543
564
CASABLANCA_DEPRECATED (" This API is deprecated and will be removed in a future release, use json::value::at() instead." )
544
565
value get (const utility::string_t &key) const ;
545
566
567
+ // / <summary>
568
+ // / Erases an element of a JSON array. Throws if index is out of bounds.
569
+ // / </summary>
570
+ // / <param name="index">The index of the element to erase in the JSON array.</param>
571
+ _ASYNCRTIMP void erase (size_t index);
572
+
573
+ // / <summary>
574
+ // / Erases an element of a JSON object. Throws if the key doesn't exist.
575
+ // / </summary>
576
+ // / <param name="key">The key of the element to erase in the JSON object.</param>
577
+ _ASYNCRTIMP void erase (const utility::string_t &key);
578
+
546
579
// / <summary>
547
580
// / Accesses an element of a JSON array. Throws when index out of bounds.
548
581
// / </summary>
@@ -841,6 +874,30 @@ namespace json
841
874
return m_elements.crend ();
842
875
}
843
876
877
+ // / <summary>
878
+ // / Deletes an element of the JSON array.
879
+ // / </summary>
880
+ // / <param name="position">A const_iterator to the element to delete.</param>
881
+ // / <returns>Iterator to the new location of the element following the erased element.</returns>
882
+ // / <remarks>GCC doesn't support erase with const_iterator on vector yet. In the future this should be changed.</remarks>
883
+ iterator erase (iterator position)
884
+ {
885
+ return m_elements.erase (position);
886
+ }
887
+
888
+ // / <summary>
889
+ // / Deletes the element at an index of the JSON array.
890
+ // / </summary>
891
+ // / <param name="index">The index of the element to delete.</param>
892
+ void erase (size_type index)
893
+ {
894
+ if (index >= m_elements.size ())
895
+ {
896
+ throw json_exception (_XPLATSTR (" index out of bounds" ));
897
+ }
898
+ m_elements.erase (m_elements.begin () + index);
899
+ }
900
+
844
901
// / <summary>
845
902
// / Accesses an element of a JSON array. Throws when index out of bounds.
846
903
// / </summary>
@@ -1031,6 +1088,32 @@ namespace json
1031
1088
return m_elements.crend ();
1032
1089
}
1033
1090
1091
+ // / <summary>
1092
+ // / Deletes an element of the JSON object.
1093
+ // / </summary>
1094
+ // / <param name="position">A const_iterator to the element to delete.</param>
1095
+ // / <returns>Iterator to the new location of the element following the erased element.</returns>
1096
+ // / <remarks>GCC doesn't support erase with const_iterator on vector yet. In the future this should be changed.</remarks>
1097
+ iterator erase (iterator position)
1098
+ {
1099
+ return m_elements.erase (position);
1100
+ }
1101
+
1102
+ // / <summary>
1103
+ // / Deletes an element of the JSON object. If the key doesn't exist, this method throws.
1104
+ // / </summary>
1105
+ // / <param name="key">The key of an element in the JSON object.</param>
1106
+ void erase (const utility::string_t &key)
1107
+ {
1108
+ auto iter = find_by_key (key);
1109
+ if (iter == m_elements.end ())
1110
+ {
1111
+ throw web::json::json_exception (_XPLATSTR (" Key not found" ));
1112
+ }
1113
+
1114
+ m_elements.erase (iter);
1115
+ }
1116
+
1034
1117
// / <summary>
1035
1118
// / Accesses an element of a JSON object. If the key doesn't exist, this method throws.
1036
1119
// / </summary>
@@ -1039,9 +1122,10 @@ namespace json
1039
1122
json::value& at (const utility::string_t & key)
1040
1123
{
1041
1124
auto iter = find_by_key (key);
1042
-
1043
- if (iter == m_elements. end () || key != (iter-> first ))
1125
+ if (iter == m_elements. end ())
1126
+ {
1044
1127
throw web::json::json_exception (_XPLATSTR (" Key not found" ));
1128
+ }
1045
1129
1046
1130
return iter->second ;
1047
1131
}
@@ -1054,9 +1138,10 @@ namespace json
1054
1138
const json::value& at (const utility::string_t & key) const
1055
1139
{
1056
1140
auto iter = find_by_key (key);
1057
-
1058
- if (iter == m_elements. end () || key != (iter-> first ))
1141
+ if (iter == m_elements. end ())
1142
+ {
1059
1143
throw web::json::json_exception (_XPLATSTR (" Key not found" ));
1144
+ }
1060
1145
1061
1146
return iter->second ;
1062
1147
}
@@ -1068,10 +1153,12 @@ namespace json
1068
1153
// / <returns>If the key exists, a reference to the value kept in the field, otherwise a newly created null value that will be stored for the given key.</returns>
1069
1154
json::value& operator [](const utility::string_t & key)
1070
1155
{
1071
- auto iter = find_by_key (key);
1156
+ auto iter = find_insert_location (key);
1072
1157
1073
- if (iter == m_elements.end () || key != (iter->first ))
1158
+ if (iter == m_elements.end () || key != iter->first )
1159
+ {
1074
1160
return m_elements.insert (iter, std::pair<utility::string_t , value>(key, value ()))->second ;
1161
+ }
1075
1162
1076
1163
return iter->second ;
1077
1164
}
@@ -1083,7 +1170,7 @@ namespace json
1083
1170
// / <returns>A const iterator to the value kept in the field.</returns>
1084
1171
const_iterator find (const utility::string_t & key) const
1085
1172
{
1086
- return find_internal (key);
1173
+ return find_by_key (key);
1087
1174
}
1088
1175
1089
1176
// / <summary>
@@ -1114,7 +1201,7 @@ namespace json
1114
1201
return p1.first < key;
1115
1202
}
1116
1203
1117
- storage_type::const_iterator find_by_key (const utility::string_t & key) const
1204
+ storage_type::iterator find_insert_location (const utility::string_t & key)
1118
1205
{
1119
1206
if (m_keep_order)
1120
1207
{
@@ -1129,7 +1216,7 @@ namespace json
1129
1216
}
1130
1217
}
1131
1218
1132
- storage_type::iterator find_by_key (const utility::string_t & key)
1219
+ storage_type::const_iterator find_by_key (const utility::string_t & key) const
1133
1220
{
1134
1221
if (m_keep_order)
1135
1222
{
@@ -1140,37 +1227,22 @@ namespace json
1140
1227
}
1141
1228
else
1142
1229
{
1143
- return std::lower_bound (m_elements.begin (), m_elements.end (), key, compare_with_key);
1230
+ auto iter = std::lower_bound (m_elements.begin (), m_elements.end (), key, compare_with_key);
1231
+ if (iter != m_elements.end () && key != iter->first )
1232
+ {
1233
+ return m_elements.end ();
1234
+ }
1235
+ return iter;
1144
1236
}
1145
1237
}
1146
1238
1147
- const json::value& at_internal (const utility::string_t & key) const
1148
- {
1149
- auto iter = find_by_key (key);
1150
-
1151
- if (iter == m_elements.end () || key != (iter->first ))
1152
- throw web::json::json_exception (_XPLATSTR (" Key not found" ));
1153
-
1154
- return iter->second ;
1155
- }
1156
-
1157
- const_iterator find_internal (const utility::string_t & key) const
1158
- {
1159
- auto iter = find_by_key (key);
1160
-
1161
- if (iter != m_elements.end () && key != (iter->first ))
1162
- return m_elements.end ();
1163
-
1164
- return iter;
1165
- }
1166
-
1167
- iterator find_internal (const utility::string_t & key)
1239
+ storage_type::iterator find_by_key (const utility::string_t & key)
1168
1240
{
1169
- auto iter = find_by_key (key);
1170
-
1171
- if (iter != m_elements. end () && key != (iter-> first ))
1241
+ auto iter = find_insert_location (key);
1242
+ if (iter != m_elements. end () && key != iter-> first )
1243
+ {
1172
1244
return m_elements.end ();
1173
-
1245
+ }
1174
1246
return iter;
1175
1247
}
1176
1248
0 commit comments