@@ -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>
@@ -843,6 +876,30 @@ namespace json
843
876
return m_elements.crend ();
844
877
}
845
878
879
+ // / <summary>
880
+ // / Deletes an element of the JSON array.
881
+ // / </summary>
882
+ // / <param name="position">A const_iterator to the element to delete.</param>
883
+ // / <returns>Iterator to the new location of the element following the erased element.</returns>
884
+ // / <remarks>GCC doesn't support erase with const_iterator on vector yet. In the future this should be changed.</remarks>
885
+ iterator erase (iterator position)
886
+ {
887
+ return m_elements.erase (position);
888
+ }
889
+
890
+ // / <summary>
891
+ // / Deletes the element at an index of the JSON array.
892
+ // / </summary>
893
+ // / <param name="index">The index of the element to delete.</param>
894
+ void erase (size_type index)
895
+ {
896
+ if (index >= m_elements.size ())
897
+ {
898
+ throw json_exception (_XPLATSTR (" index out of bounds" ));
899
+ }
900
+ m_elements.erase (m_elements.begin () + index);
901
+ }
902
+
846
903
// / <summary>
847
904
// / Accesses an element of a JSON array. Throws when index out of bounds.
848
905
// / </summary>
@@ -1035,6 +1092,32 @@ namespace json
1035
1092
return m_elements.crend ();
1036
1093
}
1037
1094
1095
+ // / <summary>
1096
+ // / Deletes an element of the JSON object.
1097
+ // / </summary>
1098
+ // / <param name="position">A const_iterator to the element to delete.</param>
1099
+ // / <returns>Iterator to the new location of the element following the erased element.</returns>
1100
+ // / <remarks>GCC doesn't support erase with const_iterator on vector yet. In the future this should be changed.</remarks>
1101
+ iterator erase (iterator position)
1102
+ {
1103
+ return m_elements.erase (position);
1104
+ }
1105
+
1106
+ // / <summary>
1107
+ // / Deletes an element of the JSON object. If the key doesn't exist, this method throws.
1108
+ // / </summary>
1109
+ // / <param name="key">The key of an element in the JSON object.</param>
1110
+ void erase (const utility::string_t &key)
1111
+ {
1112
+ auto iter = find_by_key (key);
1113
+ if (iter == m_elements.end ())
1114
+ {
1115
+ throw web::json::json_exception (_XPLATSTR (" Key not found" ));
1116
+ }
1117
+
1118
+ m_elements.erase (iter);
1119
+ }
1120
+
1038
1121
// / <summary>
1039
1122
// / Accesses an element of a JSON object. If the key doesn't exist, this method throws.
1040
1123
// / </summary>
@@ -1043,9 +1126,10 @@ namespace json
1043
1126
json::value& at (const utility::string_t & key)
1044
1127
{
1045
1128
auto iter = find_by_key (key);
1046
-
1047
- if (iter == m_elements. end () || key != (iter-> first ))
1129
+ if (iter == m_elements. end ())
1130
+ {
1048
1131
throw web::json::json_exception (_XPLATSTR (" Key not found" ));
1132
+ }
1049
1133
1050
1134
return iter->second ;
1051
1135
}
@@ -1058,9 +1142,10 @@ namespace json
1058
1142
const json::value& at (const utility::string_t & key) const
1059
1143
{
1060
1144
auto iter = find_by_key (key);
1061
-
1062
- if (iter == m_elements. end () || key != (iter-> first ))
1145
+ if (iter == m_elements. end ())
1146
+ {
1063
1147
throw web::json::json_exception (_XPLATSTR (" Key not found" ));
1148
+ }
1064
1149
1065
1150
return iter->second ;
1066
1151
}
@@ -1072,10 +1157,12 @@ namespace json
1072
1157
// / <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>
1073
1158
json::value& operator [](const utility::string_t & key)
1074
1159
{
1075
- auto iter = find_by_key (key);
1160
+ auto iter = find_insert_location (key);
1076
1161
1077
- if (iter == m_elements.end () || key != (iter->first ))
1162
+ if (iter == m_elements.end () || key != iter->first )
1163
+ {
1078
1164
return m_elements.insert (iter, std::pair<utility::string_t , value>(key, value ()))->second ;
1165
+ }
1079
1166
1080
1167
return iter->second ;
1081
1168
}
@@ -1087,7 +1174,7 @@ namespace json
1087
1174
// / <returns>A const iterator to the value kept in the field.</returns>
1088
1175
const_iterator find (const utility::string_t & key) const
1089
1176
{
1090
- return find_internal (key);
1177
+ return find_by_key (key);
1091
1178
}
1092
1179
1093
1180
// / <summary>
@@ -1118,7 +1205,7 @@ namespace json
1118
1205
return p1.first < key;
1119
1206
}
1120
1207
1121
- storage_type::const_iterator find_by_key (const utility::string_t & key) const
1208
+ storage_type::iterator find_insert_location (const utility::string_t & key)
1122
1209
{
1123
1210
if (m_keep_order)
1124
1211
{
@@ -1133,7 +1220,7 @@ namespace json
1133
1220
}
1134
1221
}
1135
1222
1136
- storage_type::iterator find_by_key (const utility::string_t & key)
1223
+ storage_type::const_iterator find_by_key (const utility::string_t & key) const
1137
1224
{
1138
1225
if (m_keep_order)
1139
1226
{
@@ -1144,37 +1231,22 @@ namespace json
1144
1231
}
1145
1232
else
1146
1233
{
1147
- return std::lower_bound (m_elements.begin (), m_elements.end (), key, compare_with_key);
1234
+ auto iter = std::lower_bound (m_elements.begin (), m_elements.end (), key, compare_with_key);
1235
+ if (iter != m_elements.end () && key != iter->first )
1236
+ {
1237
+ return m_elements.end ();
1238
+ }
1239
+ return iter;
1148
1240
}
1149
1241
}
1150
1242
1151
- const json::value& at_internal (const utility::string_t & key) const
1152
- {
1153
- auto iter = find_by_key (key);
1154
-
1155
- if (iter == m_elements.end () || key != (iter->first ))
1156
- throw web::json::json_exception (_XPLATSTR (" Key not found" ));
1157
-
1158
- return iter->second ;
1159
- }
1160
-
1161
- const_iterator find_internal (const utility::string_t & key) const
1162
- {
1163
- auto iter = find_by_key (key);
1164
-
1165
- if (iter != m_elements.end () && key != (iter->first ))
1166
- return m_elements.end ();
1167
-
1168
- return iter;
1169
- }
1170
-
1171
- iterator find_internal (const utility::string_t & key)
1243
+ storage_type::iterator find_by_key (const utility::string_t & key)
1172
1244
{
1173
- auto iter = find_by_key (key);
1174
-
1175
- if (iter != m_elements. end () && key != (iter-> first ))
1245
+ auto iter = find_insert_location (key);
1246
+ if (iter != m_elements. end () && key != iter-> first )
1247
+ {
1176
1248
return m_elements.end ();
1177
-
1249
+ }
1178
1250
return iter;
1179
1251
}
1180
1252
0 commit comments