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
3c780f0f
Commit
3c780f0f
authored
Jan 21, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
+ generic constructors for arrays and objects
parent
7afabc2b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
118 additions
and
0 deletions
+118
-0
json.h
header_only/json.h
+19
-0
json.h
src/json.h
+19
-0
json_unit.cc
test/json_unit.cc
+80
-0
No files found.
header_only/json.h
View file @
3c780f0f
...
...
@@ -151,6 +151,25 @@ class json
:
type_
(
value_type
::
number_float
),
value_
(
static_cast
<
number_float_t
>
(
n
))
{}
/// create an array object
template
<
class
V
,
typename
std
::
enable_if
<
not
std
::
is_same
<
V
,
json
::
const_iterator
>::
value
and
not
std
::
is_same
<
V
,
json
::
iterator
>::
value
and
std
::
is_constructible
<
json
,
typename
V
::
value_type
>::
value
,
int
>::
type
=
0
>
json
(
const
V
&
v
)
:
json
(
array_t
(
v
.
begin
(),
v
.
end
()))
{}
/// create a JSON object
template
<
class
V
,
typename
std
::
enable_if
<
std
::
is_constructible
<
std
::
string
,
typename
V
::
key_type
>::
value
and
std
::
is_constructible
<
json
,
typename
V
::
mapped_type
>::
value
,
int
>::
type
=
0
>
json
(
const
V
&
v
)
:
json
(
object_t
(
v
.
begin
(),
v
.
end
()))
{}
/// copy constructor
json
(
const
json
&
);
/// move constructor
...
...
src/json.h
View file @
3c780f0f
...
...
@@ -151,6 +151,25 @@ class json
:
type_
(
value_type
::
number_float
),
value_
(
static_cast
<
number_float_t
>
(
n
))
{}
/// create an array object
template
<
class
V
,
typename
std
::
enable_if
<
not
std
::
is_same
<
V
,
json
::
const_iterator
>::
value
and
not
std
::
is_same
<
V
,
json
::
iterator
>::
value
and
std
::
is_constructible
<
json
,
typename
V
::
value_type
>::
value
,
int
>::
type
=
0
>
json
(
const
V
&
v
)
:
json
(
array_t
(
v
.
begin
(),
v
.
end
()))
{}
/// create a JSON object
template
<
class
V
,
typename
std
::
enable_if
<
std
::
is_constructible
<
std
::
string
,
typename
V
::
key_type
>::
value
and
std
::
is_constructible
<
json
,
typename
V
::
mapped_type
>::
value
,
int
>::
type
=
0
>
json
(
const
V
&
v
)
:
json
(
object_t
(
v
.
begin
(),
v
.
end
()))
{}
/// copy constructor
json
(
const
json
&
);
/// move constructor
...
...
test/json_unit.cc
View file @
3c780f0f
...
...
@@ -3,6 +3,15 @@
#include "json.h"
#include <set>
#include <unordered_set>
#include <deque>
#include <list>
#include <forward_list>
#include <array>
#include <map>
#include <unordered_map>
using
json
=
nlohmann
::
json
;
TEST_CASE
(
"array"
)
...
...
@@ -324,6 +333,54 @@ TEST_CASE("array")
CHECK
(
a1
==
json
({
"one"
,
"two"
,
"three"
}));
CHECK
(
a2
==
json
({
1
,
2
,
3
,
4
}));
}
SECTION
(
"Construct from sequence objects"
)
{
std
::
vector
<
int
>
c_vector
{
1
,
2
,
3
,
4
};
json
j_vec
(
c_vector
);
CHECK
(
j_vec
.
type
()
==
json
::
value_type
::
array
);
CHECK
(
j_vec
.
size
()
==
4
);
std
::
set
<
std
::
string
>
c_set
{
"one"
,
"two"
,
"three"
,
"four"
,
"one"
};
json
j_set
(
c_set
);
CHECK
(
j_set
.
type
()
==
json
::
value_type
::
array
);
CHECK
(
j_set
.
size
()
==
4
);
std
::
unordered_set
<
std
::
string
>
c_uset
{
"one"
,
"two"
,
"three"
,
"four"
,
"one"
};
json
j_uset
(
c_uset
);
CHECK
(
j_uset
.
type
()
==
json
::
value_type
::
array
);
CHECK
(
j_uset
.
size
()
==
4
);
std
::
multiset
<
std
::
string
>
c_mset
{
"one"
,
"two"
,
"one"
,
"four"
};
json
j_mset
(
c_mset
);
CHECK
(
j_mset
.
type
()
==
json
::
value_type
::
array
);
CHECK
(
j_mset
.
size
()
==
4
);
std
::
unordered_multiset
<
std
::
string
>
c_umset
{
"one"
,
"two"
,
"one"
,
"four"
};
json
j_umset
(
c_umset
);
CHECK
(
j_umset
.
type
()
==
json
::
value_type
::
array
);
CHECK
(
j_umset
.
size
()
==
4
);
std
::
deque
<
float
>
c_deque
{
1.2
,
2.3
,
3.4
,
5.6
};
json
j_deque
(
c_deque
);
CHECK
(
j_deque
.
type
()
==
json
::
value_type
::
array
);
CHECK
(
j_deque
.
size
()
==
4
);
std
::
list
<
bool
>
c_list
{
true
,
true
,
false
,
true
};
json
j_list
(
c_list
);
CHECK
(
j_list
.
type
()
==
json
::
value_type
::
array
);
CHECK
(
j_list
.
size
()
==
4
);
std
::
forward_list
<
int64_t
>
c_flist
{
12345678909876
,
23456789098765
,
34567890987654
,
45678909876543
};
json
j_flist
(
c_flist
);
CHECK
(
j_flist
.
type
()
==
json
::
value_type
::
array
);
CHECK
(
j_flist
.
size
()
==
4
);
std
::
array
<
unsigned
long
,
4
>
c_array
{{
1
,
2
,
3
,
4
}};
json
j_array
(
c_array
);
CHECK
(
j_array
.
type
()
==
json
::
value_type
::
array
);
CHECK
(
j_array
.
size
()
==
4
);
}
}
TEST_CASE
(
"object"
)
...
...
@@ -744,6 +801,29 @@ TEST_CASE("object")
CHECK
(
o1
==
json
({
{
"one"
,
"eins"
},
{
"two"
,
"zwei"
}
}));
CHECK
(
o2
==
json
({
{
"one"
,
1
},
{
"two"
,
2
}
}));
}
SECTION
(
"Construct from sequence objects"
)
{
std
::
map
<
std
::
string
,
int
>
c_map
{
{
"one"
,
1
},
{
"two"
,
2
},
{
"three"
,
3
}
};
json
j_map
(
c_map
);
CHECK
(
j_map
.
type
()
==
json
::
value_type
::
object
);
CHECK
(
j_map
.
size
()
==
3
);
std
::
unordered_map
<
const
char
*
,
float
>
c_umap
{
{
"one"
,
1.2
},
{
"two"
,
2.3
},
{
"three"
,
3.4
}
};
json
j_umap
(
c_umap
);
CHECK
(
j_umap
.
type
()
==
json
::
value_type
::
object
);
CHECK
(
j_umap
.
size
()
==
3
);
std
::
multimap
<
std
::
string
,
bool
>
c_mmap
{
{
"one"
,
true
},
{
"two"
,
true
},
{
"three"
,
false
},
{
"three"
,
true
}
};
json
j_mmap
(
c_mmap
);
CHECK
(
j_mmap
.
type
()
==
json
::
value_type
::
object
);
CHECK
(
j_mmap
.
size
()
==
3
);
std
::
unordered_multimap
<
std
::
string
,
bool
>
c_ummap
{
{
"one"
,
true
},
{
"two"
,
true
},
{
"three"
,
false
},
{
"three"
,
true
}
};
json
j_ummap
(
c_ummap
);
CHECK
(
j_ummap
.
type
()
==
json
::
value_type
::
object
);
CHECK
(
j_ummap
.
size
()
==
3
);
}
}
TEST_CASE
(
"null"
)
...
...
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