Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
json
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
json
Commits
8c6bb04d
Commit
8c6bb04d
authored
Feb 10, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added width feature / more test cases
parent
4cd341d4
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
70 additions
and
7 deletions
+70
-7
json.hpp
src/json.hpp
+14
-4
json.hpp.re2c
src/json.hpp.re2c
+8
-2
unit.cpp
test/unit.cpp
+48
-1
No files found.
src/json.hpp
View file @
8c6bb04d
...
@@ -1264,14 +1264,20 @@ class basic_json
...
@@ -1264,14 +1264,20 @@ class basic_json
/// serialize to stream
/// serialize to stream
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
o
,
const
basic_json
&
j
)
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
o
,
const
basic_json
&
j
)
{
{
o
<<
j
.
dump
();
// read width member and use it as indentation parameter if nonzero
const
int
indentation
=
(
o
.
width
()
==
0
)
?
-
1
:
o
.
width
();
o
<<
j
.
dump
(
indentation
);
return
o
;
return
o
;
}
}
/// serialize to stream
/// serialize to stream
friend
std
::
ostream
&
operator
>>
(
const
basic_json
&
j
,
std
::
ostream
&
o
)
friend
std
::
ostream
&
operator
>>
(
const
basic_json
&
j
,
std
::
ostream
&
o
)
{
{
o
<<
j
.
dump
();
// read width member and use it as indentation parameter if nonzero
const
int
indentation
=
(
o
.
width
()
==
0
)
?
-
1
:
o
.
width
();
o
<<
j
.
dump
(
indentation
);
return
o
;
return
o
;
}
}
...
@@ -1315,24 +1321,28 @@ class basic_json
...
@@ -1315,24 +1321,28 @@ class basic_json
{
{
return
"null"
;
return
"null"
;
}
}
case
(
value_t
:
:
object
)
:
case
(
value_t
:
:
object
)
:
{
{
return
"object"
;
return
"object"
;
}
}
case
(
value_t
:
:
array
)
:
case
(
value_t
:
:
array
)
:
{
{
return
"array"
;
return
"array"
;
}
}
case
(
value_t
:
:
string
)
:
case
(
value_t
:
:
string
)
:
{
{
return
"string"
;
return
"string"
;
}
}
case
(
value_t
:
:
boolean
)
:
case
(
value_t
:
:
boolean
)
:
{
{
return
"boolean"
;
return
"boolean"
;
}
}
case
(
value_t
:
:
number_integer
)
:
case
(
value_t
:
:
number_float
)
:
default
:
{
{
return
"number"
;
return
"number"
;
}
}
...
...
src/json.hpp.re2c
View file @
8c6bb04d
...
@@ -1264,14 +1264,20 @@ class basic_json
...
@@ -1264,14 +1264,20 @@ class basic_json
/// serialize to stream
/// serialize to stream
friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
{
{
o << j.dump();
// read width member and use it as indentation parameter if nonzero
const int indentation = (o.width() == 0) ? -1 : o.width();
o << j.dump(indentation);
return o;
return o;
}
}
/// serialize to stream
/// serialize to stream
friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
{
{
o << j.dump();
// read width member and use it as indentation parameter if nonzero
const int indentation = (o.width() == 0) ? -1 : o.width();
o << j.dump(indentation);
return o;
return o;
}
}
...
...
test/unit.cpp
View file @
8c6bb04d
...
@@ -7,6 +7,7 @@ using nlohmann::json;
...
@@ -7,6 +7,7 @@ using nlohmann::json;
#include <array>
#include <array>
#include <deque>
#include <deque>
#include <forward_list>
#include <forward_list>
#include <iomanip>
#include <list>
#include <list>
#include <map>
#include <map>
#include <set>
#include <set>
...
@@ -1045,10 +1046,12 @@ TEST_CASE("object inspection")
...
@@ -1045,10 +1046,12 @@ TEST_CASE("object inspection")
{
{
json
j
{{
"object"
,
json
::
object
()},
{
"array"
,
{
1
,
2
,
3
,
4
}},
{
"number"
,
42
},
{
"boolean"
,
false
},
{
"null"
,
nullptr
},
{
"string"
,
"Hello world"
}
};
json
j
{{
"object"
,
json
::
object
()},
{
"array"
,
{
1
,
2
,
3
,
4
}},
{
"number"
,
42
},
{
"boolean"
,
false
},
{
"null"
,
nullptr
},
{
"string"
,
"Hello world"
}
};
SECTION
(
"no indent"
)
SECTION
(
"no indent
/ indent=-1
"
)
{
{
CHECK
(
j
.
dump
()
==
CHECK
(
j
.
dump
()
==
"{
\"
array
\"
:[1,2,3,4],
\"
boolean
\"
:false,
\"
null
\"
:null,
\"
number
\"
:42,
\"
object
\"
:{},
\"
string
\"
:
\"
Hello world
\"
}"
);
"{
\"
array
\"
:[1,2,3,4],
\"
boolean
\"
:false,
\"
null
\"
:null,
\"
number
\"
:42,
\"
object
\"
:{},
\"
string
\"
:
\"
Hello world
\"
}"
);
CHECK
(
j
.
dump
()
==
j
.
dump
(
-
1
));
}
}
SECTION
(
"indent=0"
)
SECTION
(
"indent=0"
)
...
@@ -2152,12 +2155,25 @@ TEST_CASE("element access")
...
@@ -2152,12 +2155,25 @@ TEST_CASE("element access")
SECTION
(
"access within bounds"
)
SECTION
(
"access within bounds"
)
{
{
CHECK
(
j
[
"integer"
]
==
json
(
1
));
CHECK
(
j
[
"integer"
]
==
json
(
1
));
CHECK
(
j
[
json
::
object_t
::
key_type
(
"integer"
)]
==
j
[
"integer"
]);
CHECK
(
j
[
"boolean"
]
==
json
(
true
));
CHECK
(
j
[
"boolean"
]
==
json
(
true
));
CHECK
(
j
[
json
::
object_t
::
key_type
(
"boolean"
)]
==
j
[
"boolean"
]);
CHECK
(
j
[
"null"
]
==
json
(
nullptr
));
CHECK
(
j
[
"null"
]
==
json
(
nullptr
));
CHECK
(
j
[
json
::
object_t
::
key_type
(
"null"
)]
==
j
[
"null"
]);
CHECK
(
j
[
"string"
]
==
json
(
"hello world"
));
CHECK
(
j
[
"string"
]
==
json
(
"hello world"
));
CHECK
(
j
[
json
::
object_t
::
key_type
(
"string"
)]
==
j
[
"string"
]);
CHECK
(
j
[
"floating"
]
==
json
(
42.23
));
CHECK
(
j
[
"floating"
]
==
json
(
42.23
));
CHECK
(
j
[
json
::
object_t
::
key_type
(
"floating"
)]
==
j
[
"floating"
]);
CHECK
(
j
[
"object"
]
==
json
(
json
::
object
()));
CHECK
(
j
[
"object"
]
==
json
(
json
::
object
()));
CHECK
(
j
[
json
::
object_t
::
key_type
(
"object"
)]
==
j
[
"object"
]);
CHECK
(
j
[
"array"
]
==
json
({
1
,
2
,
3
}));
CHECK
(
j
[
"array"
]
==
json
({
1
,
2
,
3
}));
CHECK
(
j
[
json
::
object_t
::
key_type
(
"array"
)]
==
j
[
"array"
]);
}
}
SECTION
(
"access on non-object type"
)
SECTION
(
"access on non-object type"
)
...
@@ -2166,36 +2182,42 @@ TEST_CASE("element access")
...
@@ -2166,36 +2182,42 @@ TEST_CASE("element access")
{
{
json
j_nonobject
(
json
::
value_t
::
null
);
json
j_nonobject
(
json
::
value_t
::
null
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
runtime_error
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
runtime_error
);
CHECK_THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
runtime_error
);
}
}
SECTION
(
"boolean"
)
SECTION
(
"boolean"
)
{
{
json
j_nonobject
(
json
::
value_t
::
boolean
);
json
j_nonobject
(
json
::
value_t
::
boolean
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
runtime_error
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
runtime_error
);
CHECK_THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
runtime_error
);
}
}
SECTION
(
"string"
)
SECTION
(
"string"
)
{
{
json
j_nonobject
(
json
::
value_t
::
string
);
json
j_nonobject
(
json
::
value_t
::
string
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
runtime_error
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
runtime_error
);
CHECK_THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
runtime_error
);
}
}
SECTION
(
"array"
)
SECTION
(
"array"
)
{
{
json
j_nonobject
(
json
::
value_t
::
array
);
json
j_nonobject
(
json
::
value_t
::
array
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
runtime_error
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
runtime_error
);
CHECK_THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
runtime_error
);
}
}
SECTION
(
"number (integer)"
)
SECTION
(
"number (integer)"
)
{
{
json
j_nonobject
(
json
::
value_t
::
number_integer
);
json
j_nonobject
(
json
::
value_t
::
number_integer
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
runtime_error
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
runtime_error
);
CHECK_THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
runtime_error
);
}
}
SECTION
(
"number (floating-point)"
)
SECTION
(
"number (floating-point)"
)
{
{
json
j_nonobject
(
json
::
value_t
::
number_float
);
json
j_nonobject
(
json
::
value_t
::
number_float
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
runtime_error
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
runtime_error
);
CHECK_THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
runtime_error
);
}
}
}
}
}
}
...
@@ -3826,19 +3848,44 @@ TEST_CASE("serialization")
...
@@ -3826,19 +3848,44 @@ TEST_CASE("serialization")
{
{
SECTION
(
"operator<<"
)
SECTION
(
"operator<<"
)
{
{
SECTION
(
"no given width"
)
{
std
::
stringstream
ss
;
std
::
stringstream
ss
;
json
j
=
{
"foo"
,
1
,
2
,
3
,
false
,
{{
"one"
,
1
}}};
json
j
=
{
"foo"
,
1
,
2
,
3
,
false
,
{{
"one"
,
1
}}};
ss
<<
j
;
ss
<<
j
;
CHECK
(
ss
.
str
()
==
"[
\"
foo
\"
,1,2,3,false,{
\"
one
\"
:1}]"
);
CHECK
(
ss
.
str
()
==
"[
\"
foo
\"
,1,2,3,false,{
\"
one
\"
:1}]"
);
}
}
SECTION
(
"given width"
)
{
std
::
stringstream
ss
;
json
j
=
{
"foo"
,
1
,
2
,
3
,
false
,
{{
"one"
,
1
}}};
ss
<<
std
::
setw
(
4
)
<<
j
;
CHECK
(
ss
.
str
()
==
"[
\n
\"
foo
\"
,
\n
1,
\n
2,
\n
3,
\n
false,
\n
{
\n
\"
one
\"
: 1
\n
}
\n
]"
);
}
}
SECTION
(
"operator>>"
)
SECTION
(
"operator>>"
)
{
{
SECTION
(
"no given width"
)
{
std
::
stringstream
ss
;
std
::
stringstream
ss
;
json
j
=
{
"foo"
,
1
,
2
,
3
,
false
,
{{
"one"
,
1
}}};
json
j
=
{
"foo"
,
1
,
2
,
3
,
false
,
{{
"one"
,
1
}}};
j
>>
ss
;
j
>>
ss
;
CHECK
(
ss
.
str
()
==
"[
\"
foo
\"
,1,2,3,false,{
\"
one
\"
:1}]"
);
CHECK
(
ss
.
str
()
==
"[
\"
foo
\"
,1,2,3,false,{
\"
one
\"
:1}]"
);
}
}
SECTION
(
"given width"
)
{
std
::
stringstream
ss
;
json
j
=
{
"foo"
,
1
,
2
,
3
,
false
,
{{
"one"
,
1
}}};
ss
.
width
(
4
);
j
>>
ss
;
CHECK
(
ss
.
str
()
==
"[
\n
\"
foo
\"
,
\n
1,
\n
2,
\n
3,
\n
false,
\n
{
\n
\"
one
\"
: 1
\n
}
\n
]"
);
}
}
}
}
TEST_CASE
(
"deserialization"
)
TEST_CASE
(
"deserialization"
)
...
...
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