Skip to content

Commit cec2ec3

Browse files
committed
Rename json_pointer.get to value
1 parent 6119bcb commit cec2ec3

File tree

4 files changed

+117
-23
lines changed

4 files changed

+117
-23
lines changed

src/examples/usage.h

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,30 @@
66
#include <format/json.h>
77
#include <format/json_pointer.h>
88

9+
using namespace format;
10+
911
void
1012
usage ()
1113
{
14+
// Create a JSON pointer object
15+
json_pointer jp (L"/foo/1");
16+
1217
// Create a JSON object
13-
format::json j = L"{ \"foo\": [\"bar\", \"baz\"],\
14-
\"\": 0,\
15-
\"a/b\": 1,\
16-
\"c%d\": 2,\
17-
\"e^f\": 3,\
18-
\"g|h\": 4,\
19-
\"i\\j\": 5,\
20-
\" \": 7,\
21-
\"m~n\": 8 }";
18+
json j = L"{ \"foo\": [\"bar\", \"baz\"],\
19+
\"\": 0,\
20+
\"a/b\": 1,\
21+
\"c%d\": 2,\
22+
\"e^f\": 3,\
23+
\"g|h\": 4,\
24+
\"i\\j\": 5,\
25+
\" \": 7,\
26+
\"m~n\": 8 }";
27+
28+
// Get the value the pointer refers to
29+
value & v = jp.value (j);
30+
31+
std::wcout << v.as<const wchar_t *>() << std::endl;
32+
// ouput: baz
2233

2334
// Create an array of JSON pointers
2435
std::array<format::json_pointer, 13> jp_list = {
@@ -42,16 +53,16 @@ usage ()
4253
for (auto& jp : jp_list)
4354
{
4455
try {
45-
// Look for the value in the JSON object.
46-
format::json::value & v = jp.get (j);
56+
// Get the value the pointer refers to
57+
json::value & v = jp.value (j);
4758

4859
// If value is not found, object type is undefined
49-
if (v.type () == format::json::value::undefined_t)
60+
if (v.type () == value::undefined_t)
5061
std::wcout << "Value not found" << std::endl;
5162
else
5263
std::wcout << v.stringify () << std::endl;
5364

54-
} catch (format::json_pointer_error & e) {
65+
} catch (json_pointer_error & e) {
5566
// Invalid pointer syntax or
5667
// a pointer that references a nonexistent value
5768
std::wcerr << e.what () << std::endl;

src/json_pointer/json_pointer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ format::json_pointer::~json_pointer ()
1515
}
1616

1717
format::value &
18-
format::json_pointer::get (json & json) const
18+
format::json_pointer::value (json & json) const
1919
{
2020
return _point (json, _json_pointer.cbegin ());
2121
}
2222

2323
format::value &
24-
format::json_pointer::get (const wchar_t * const json_text) const
24+
format::json_pointer::value (const wchar_t * const json_text) const
2525
{
2626
if (json_text == nullptr)
2727
throw json_pointer_error ("JSON text is null");
2828

2929
format::json json (json_text);
30-
return get (json);
30+
return value (json);
3131
}
3232

3333
std::size_t
@@ -49,7 +49,7 @@ format::json_pointer::_parse (const wchar_t * const json_pointer)
4949
}
5050

5151
format::value &
52-
format::json_pointer::_point (value & v, std::vector<const wchar_t *>::const_iterator cur) const
52+
format::json_pointer::_point (class value & v, std::vector<const wchar_t *>::const_iterator cur) const
5353
{
5454
if (cur == _json_pointer.cend ())
5555
return v;

src/json_pointer/json_pointer.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ namespace format
4545
~json_pointer ();
4646

4747
/**
48-
* @brief get
48+
* @brief value
4949
* @param json FIXME: THIS MUST BE CONST. JSON SHOULD HAVE operator [](const wchar_t *) const !!!
5050
* @return
5151
*/
52-
format::value & get (format::json &) const;
52+
format::value & value (format::json &) const;
5353

5454
/**
55-
* @brief get
55+
* @brief value
5656
* @param json
5757
* @return
5858
*/
59-
format::value & get (const wchar_t * const) const;
59+
format::value & value (const wchar_t * const) const;
6060

6161
protected:
6262
/**
@@ -78,7 +78,7 @@ namespace format
7878
* @param end
7979
* @return
8080
*/
81-
format::value & _point (value &, std::vector<const wchar_t *>::const_iterator) const;
81+
format::value & _point (class value &, std::vector<const wchar_t *>::const_iterator) const;
8282

8383
/**
8484
* @brief The reference_token class

src/test/json_pointer_test.h

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ namespace format
254254
TEST_IT_START
255255

256256
json_pointer jp ((*it).ref_token);
257-
format::value & v = jp.get (j);
257+
format::value & v = jp.value (j);
258258

259259
ASSERT_EQ ((*it).type, v.type ());
260260

@@ -266,6 +266,89 @@ namespace format
266266

267267
TEST_IT_END
268268
}
269+
270+
/* TODO: ???? void
271+
test_point ()
272+
{
273+
format::json j ( L"{ \"foo\": [\"bar\", \"baz\"],\
274+
\"\": 0,\
275+
\"a/b\": 1,\
276+
\"c%d\": 2,\
277+
\"e^f\": 3,\
278+
\"g|h\": 4,\
279+
\"i\\j\": 5,\
280+
\" \": 7,\
281+
\"m~n\": 8 }" );
282+
283+
// FIXME: \"k\"l\": 6 parse error
284+
285+
struct assert
286+
{
287+
const wchar_t *ref_token;
288+
value::value_t type;
289+
long num_val;
290+
int assert_status;
291+
};
292+
293+
std::vector<struct assert > test = {
294+
{ L"", value::value_t::object_t, 0, PASS },
295+
{ L"/foo", value::value_t::array_t, 0, PASS },
296+
{ L"/foo/-", value::value_t::undefined_t, 0, PASS },
297+
{ L"/not", value::value_t::undefined_t, 0, PASS },
298+
{ L"/not/found", value::value_t::undefined_t, 0, FAIL },
299+
};
300+
301+
TEST_IT_START
302+
303+
format::value & v = j.point ((*it).ref_token);
304+
ASSERT_EQUAL_IDX ("point type", (*it).type, v.type ());
305+
306+
if (v.type () == value::number_t)
307+
{
308+
format::number & n = static_cast<format::number &> (v);
309+
ASSERT_EQUAL_IDX ("point numeric value", (*it).num_val, static_cast<long> (n.get ()));
310+
}
311+
312+
TEST_IT_END
313+
} */
314+
315+
/* TODO: void
316+
test_point_assign ()
317+
{
318+
format::json j ( L"{ \"foo\": [],\
319+
\"bar\": {} }" );
320+
321+
struct assert
322+
{
323+
const wchar_t *ref_token[2];
324+
long new_val;
325+
value::value_t type;
326+
int assert_status;
327+
};
328+
329+
std::vector<struct assert > test = {
330+
{ { L"/foo/-", L"/foo/0" }, 1, value::value_t::number_t, PASS },
331+
{ { L"/foo/0", L"/foo/0" }, 2, value::value_t::number_t, PASS },
332+
{ { L"/bar/quux", L"/bar/quux" }, 3, value::value_t::number_t, PASS },
333+
{ { L"/not/found", L"/not/found" }, 4, value::value_t::undefined_t, FAIL },
334+
};
335+
336+
TEST_IT_START
337+
338+
j.point ((*it).ref_token[0]) = (*it).new_val;
339+
format::value & v = j.point ((*it).ref_token[1]);
340+
341+
ASSERT_EQUAL_IDX ("point type", (*it).type, v.type ());
342+
343+
if (v.type () == value::number_t)
344+
{
345+
format::number & n = static_cast<format::number &> (v);
346+
ASSERT_EQUAL_IDX ("point numeric value", (*it).new_val, static_cast<long> (n.get ()));
347+
}
348+
349+
TEST_IT_END
350+
} */
351+
269352
} // namespace test
270353
} // namespace format
271354

0 commit comments

Comments
 (0)