Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
afd02eee
Commit
afd02eee
authored
Feb 18, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed [] operator; added README
parent
ca981270
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
621 additions
and
2 deletions
+621
-2
README.md
README.md
+365
-0
src/json.hpp
src/json.hpp
+18
-0
src/json.hpp.re2c
src/json.hpp.re2c
+18
-0
test/unit.cpp
test/unit.cpp
+220
-2
No files found.
README.md
0 → 100644
View file @
afd02eee
This diff is collapsed.
Click to expand it.
src/json.hpp
View file @
afd02eee
...
...
@@ -760,6 +760,15 @@ class basic_json
/// access specified element
inline
reference
operator
[](
const
typename
object_t
::
key_type
&
key
)
{
// implicitly convert null to object
if
(
m_type
==
value_t
::
null
)
{
m_type
=
value_t
::
object
;
Allocator
<
object_t
>
alloc
;
m_value
.
object
=
alloc
.
allocate
(
1
);
alloc
.
construct
(
m_value
.
object
);
}
// at only works for objects
if
(
m_type
!=
value_t
::
object
)
{
...
...
@@ -785,6 +794,15 @@ class basic_json
template
<
typename
T
,
size_t
n
>
inline
reference
operator
[](
const
T
(
&
key
)[
n
])
{
// implicitly convert null to object
if
(
m_type
==
value_t
::
null
)
{
m_type
=
value_t
::
object
;
Allocator
<
object_t
>
alloc
;
m_value
.
object
=
alloc
.
allocate
(
1
);
alloc
.
construct
(
m_value
.
object
);
}
// at only works for objects
if
(
m_type
!=
value_t
::
object
)
{
...
...
src/json.hpp.re2c
View file @
afd02eee
...
...
@@ -760,6 +760,15 @@ class basic_json
/// access specified element
inline reference operator[](const typename object_t::key_type& key)
{
// implicitly convert null to object
if (m_type == value_t::null)
{
m_type = value_t::object;
Allocator<object_t> alloc;
m_value.object = alloc.allocate(1);
alloc.construct(m_value.object);
}
// at only works for objects
if (m_type != value_t::object)
{
...
...
@@ -785,6 +794,15 @@ class basic_json
template<typename T, size_t n>
inline reference operator[](const T (&key)[n])
{
// implicitly convert null to object
if (m_type == value_t::null)
{
m_type = value_t::object;
Allocator<object_t> alloc;
m_value.object = alloc.allocate(1);
alloc.construct(m_value.object);
}
// at only works for objects
if (m_type != value_t::object)
{
...
...
test/unit.cpp
View file @
afd02eee
...
...
@@ -2212,8 +2212,8 @@ TEST_CASE("element access")
{
json
j_nonobject
(
json
::
value_t
::
null
);
const
json
j_const_nonobject
(
j_nonobject
);
CHECK_
THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
runtime_error
);
CHECK_
THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
runtime_error
);
CHECK_
NOTHROW
(
j_nonobject
[
"foo"
]
);
CHECK_
NOTHROW
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)]
);
CHECK_THROWS_AS
(
j_const_nonobject
[
"foo"
],
std
::
runtime_error
);
CHECK_THROWS_AS
(
j_const_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
runtime_error
);
}
...
...
@@ -5914,3 +5914,221 @@ TEST_CASE("parser class")
CHECK_THROWS_AS
(
json
::
parse
(
"
\"\\
uD80C
\\
uFFFF
\"
"
),
std
::
invalid_argument
);
}
}
TEST_CASE
(
"README"
,
"[hide]"
)
{
{
// create an empty structure (null)
json
j
;
// add a number that is stored as double (note the implicit conversion of j to an object)
j
[
"pi"
]
=
3.141
;
// add a Boolean that is stored as bool
j
[
"happy"
]
=
true
;
// add a string that is stored as std::string
j
[
"name"
]
=
"Niels"
;
// add another null object by passing nullptr
j
[
"nothing"
]
=
nullptr
;
// add an object inside the object
j
[
"answer"
][
"everything"
]
=
42
;
// add an array that is stored as std::vector (using an initializer list)
j
[
"list"
]
=
{
1
,
0
,
2
};
// add another object (using an initializer list of pairs)
j
[
"object"
]
=
{
{
"currency"
,
"USD"
},
{
"value"
,
42.99
}
};
// instead, you could also write (which looks very similar to the JSON above)
json
j2
=
{
{
"pi"
,
3.141
},
{
"happy"
,
true
},
{
"name"
,
"Niels"
},
{
"nothing"
,
nullptr
},
{
"answer"
,
{
{
"everything"
,
42
}
}
},
{
"list"
,
{
1
,
0
,
2
}},
{
"object"
,
{
{
"currency"
,
"USD"
},
{
"value"
,
42.99
}
}
}
};
}
{
// ways to express the empty array []
json
empty_array_implicit
=
{{}};
json
empty_array_explicit
=
json
::
array
();
// a way to express the empty object {}
json
empty_object_explicit
=
json
::
object
();
// a way to express an _array_ of key/value pairs [["currency", "USD"], ["value", 42.99]]
json
array_not_object
=
{
json
::
array
({
"currency"
,
"USD"
}),
json
::
array
({
"value"
,
42.99
})
};
}
{
// create object from string literal
json
j
=
"{
\"
happy
\"
: true,
\"
pi
\"
: 3.141 }"
_json
;
// or even nicer (thanks http://isocpp.org/blog/2015/01/json-for-modern-cpp)
auto
j2
=
R"(
{
"happy": true,
"pi": 3.141
}
)"
_json
;
// or explicitly
auto
j3
=
json
::
parse
(
"{
\"
happy
\"
: true,
\"
pi
\"
: 3.141 }"
);
// explicit conversion to string
std
::
string
s
=
j
.
dump
();
// {\"happy\":true,\"pi\":3.141}
// serialization with pretty printing
// pass in the amount of spaces to indent
std
::
cout
<<
j
.
dump
(
4
)
<<
std
::
endl
;
// {
// "happy": true,
// "pi": 3.141
// }
std
::
cout
<<
std
::
setw
(
2
)
<<
j
<<
std
::
endl
;
}
{
// create an array using push_back
json
j
;
j
.
push_back
(
"foo"
);
j
.
push_back
(
1
);
j
.
push_back
(
true
);
// iterate the array
for
(
json
::
iterator
it
=
j
.
begin
();
it
!=
j
.
end
();
++
it
)
{
std
::
cout
<<
*
it
<<
'\n'
;
}
// range-based for
for
(
auto
element
:
j
)
{
std
::
cout
<<
element
<<
'\n'
;
}
// getter/setter
const
std
::
string
tmp
=
j
[
0
];
j
[
1
]
=
42
;
bool
foo
=
j
.
at
(
2
);
// other stuff
j
.
size
();
// 3 entries
j
.
empty
();
// false
j
.
type
();
// json::value_t::array
j
.
clear
();
// the array is empty again
// comparison
j
==
"[
\"
foo
\"
, 1, true]"
_json
;
// true
// create an object
json
o
;
o
[
"foo"
]
=
23
;
o
[
"bar"
]
=
false
;
o
[
"baz"
]
=
3.141
;
// find an entry
if
(
o
.
find
(
"foo"
)
!=
o
.
end
())
{
// there is an entry with key "foo"
}
}
{
std
::
vector
<
int
>
c_vector
{
1
,
2
,
3
,
4
};
json
j_vec
(
c_vector
);
// [1, 2, 3, 4]
std
::
deque
<
float
>
c_deque
{
1.2
,
2.3
,
3.4
,
5.6
};
json
j_deque
(
c_deque
);
// [1.2, 2.3, 3.4, 5.6]
std
::
list
<
bool
>
c_list
{
true
,
true
,
false
,
true
};
json
j_list
(
c_list
);
// [true, true, false, true]
std
::
forward_list
<
int64_t
>
c_flist
{
12345678909876
,
23456789098765
,
34567890987654
,
45678909876543
};
json
j_flist
(
c_flist
);
// [12345678909876, 23456789098765, 34567890987654, 45678909876543]
std
::
array
<
unsigned
long
,
4
>
c_array
{{
1
,
2
,
3
,
4
}};
json
j_array
(
c_array
);
// [1, 2, 3, 4]
std
::
set
<
std
::
string
>
c_set
{
"one"
,
"two"
,
"three"
,
"four"
,
"one"
};
json
j_set
(
c_set
);
// only one entry for "one" is used
// ["four", "one", "three", "two"]
std
::
unordered_set
<
std
::
string
>
c_uset
{
"one"
,
"two"
,
"three"
,
"four"
,
"one"
};
json
j_uset
(
c_uset
);
// only one entry for "one" is used
// maybe ["two", "three", "four", "one"]
std
::
multiset
<
std
::
string
>
c_mset
{
"one"
,
"two"
,
"one"
,
"four"
};
json
j_mset
(
c_mset
);
// only one entry for "one" is used
// maybe ["one", "two", "four"]
std
::
unordered_multiset
<
std
::
string
>
c_umset
{
"one"
,
"two"
,
"one"
,
"four"
};
json
j_umset
(
c_umset
);
// both entries for "one" are used
// maybe ["one", "two", "one", "four"]
}
{
std
::
map
<
std
::
string
,
int
>
c_map
{
{
"one"
,
1
},
{
"two"
,
2
},
{
"three"
,
3
}
};
json
j_map
(
c_map
);
// {"one": 1, "two": 2, "three": 3}
std
::
unordered_map
<
const
char
*
,
float
>
c_umap
{
{
"one"
,
1.2
},
{
"two"
,
2.3
},
{
"three"
,
3.4
}
};
json
j_umap
(
c_umap
);
// {"one": 1.2, "two": 2.3, "three": 3.4}
std
::
multimap
<
std
::
string
,
bool
>
c_mmap
{
{
"one"
,
true
},
{
"two"
,
true
},
{
"three"
,
false
},
{
"three"
,
true
}
};
json
j_mmap
(
c_mmap
);
// only one entry for key "three" is used
// maybe {"one": true, "two": true, "three": true}
std
::
unordered_multimap
<
std
::
string
,
bool
>
c_ummap
{
{
"one"
,
true
},
{
"two"
,
true
},
{
"three"
,
false
},
{
"three"
,
true
}
};
json
j_ummap
(
c_ummap
);
// only one entry for key "three" is used
// maybe {"one": true, "two": true, "three": true}
}
{
/// strings
std
::
string
s1
=
"Hello, world!"
;
json
js
=
s1
;
std
::
string
s2
=
js
;
// Booleans
bool
b1
=
true
;
json
jb
=
b1
;
bool
b2
=
jb
;
// numbers
int
i
=
42
;
json
jn
=
i
;
double
f
=
jn
;
// etc.
std
::
string
vs
=
js
.
get
<
std
::
string
>
();
bool
vb
=
jb
.
get
<
bool
>
();
int
vi
=
jn
.
get
<
int
>
();
// etc.
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment