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
3e15b551
Commit
3e15b551
authored
Jan 17, 2017
by
Théo DELRIEU
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
run make pretty
parent
fbac056c
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
44 additions
and
44 deletions
+44
-44
json.hpp
src/json.hpp
+0
-0
json.hpp.re2c
src/json.hpp.re2c
+0
-0
unit-conversions.cpp
test/src/unit-conversions.cpp
+0
-0
unit-noexcept.cpp
test/src/unit-noexcept.cpp
+2
-2
unit-regression.cpp
test/src/unit-regression.cpp
+1
-1
unit-udt.cpp
test/src/unit-udt.cpp
+41
-41
No files found.
src/json.hpp
View file @
3e15b551
This diff is collapsed.
Click to expand it.
src/json.hpp.re2c
View file @
3e15b551
This diff is collapsed.
Click to expand it.
test/src/unit-conversions.cpp
View file @
3e15b551
test/src/unit-noexcept.cpp
View file @
3e15b551
...
@@ -11,8 +11,8 @@ enum test
...
@@ -11,8 +11,8 @@ enum test
struct
pod
{};
struct
pod
{};
struct
pod_bis
{};
struct
pod_bis
{};
void
to_json
(
json
&
,
pod
)
noexcept
;
void
to_json
(
json
&
,
pod
)
noexcept
;
void
to_json
(
json
&
,
pod_bis
);
void
to_json
(
json
&
,
pod_bis
);
void
from_json
(
const
json
&
,
pod
)
noexcept
;
void
from_json
(
const
json
&
,
pod
)
noexcept
;
void
from_json
(
const
json
&
,
pod_bis
);
void
from_json
(
const
json
&
,
pod_bis
);
static
json
j
;
static
json
j
;
...
...
test/src/unit-regression.cpp
View file @
3e15b551
...
@@ -63,7 +63,7 @@ TEST_CASE("regression tests")
...
@@ -63,7 +63,7 @@ TEST_CASE("regression tests")
SECTION
(
"pull request #71 - handle enum type"
)
SECTION
(
"pull request #71 - handle enum type"
)
{
{
enum
{
t
=
0
,
u
=
1
};
enum
{
t
=
0
,
u
=
1
};
json
j
=
json
::
array
();
json
j
=
json
::
array
();
j
.
push_back
(
t
);
j
.
push_back
(
t
);
...
...
test/src/unit-udt.cpp
View file @
3e15b551
...
@@ -114,22 +114,22 @@ void to_json(Json& j, country c)
...
@@ -114,22 +114,22 @@ void to_json(Json& j, country c)
}
}
template
<
typename
Json
>
template
<
typename
Json
>
void
to_json
(
Json
&
j
,
const
person
&
p
)
void
to_json
(
Json
&
j
,
const
person
&
p
)
{
{
j
=
Json
{{
"age"
,
p
.
m_age
},
{
"name"
,
p
.
m_name
},
{
"country"
,
p
.
m_country
}};
j
=
Json
{{
"age"
,
p
.
m_age
},
{
"name"
,
p
.
m_name
},
{
"country"
,
p
.
m_country
}};
}
}
void
to_json
(
nlohmann
::
json
&
j
,
const
address
&
a
)
void
to_json
(
nlohmann
::
json
&
j
,
const
address
&
a
)
{
{
j
=
a
.
m_val
;
j
=
a
.
m_val
;
}
}
void
to_json
(
nlohmann
::
json
&
j
,
const
contact
&
c
)
void
to_json
(
nlohmann
::
json
&
j
,
const
contact
&
c
)
{
{
j
=
json
{{
"person"
,
c
.
m_person
},
{
"address"
,
c
.
m_address
}};
j
=
json
{{
"person"
,
c
.
m_person
},
{
"address"
,
c
.
m_address
}};
}
}
void
to_json
(
nlohmann
::
json
&
j
,
const
contact_book
&
cb
)
void
to_json
(
nlohmann
::
json
&
j
,
const
contact_book
&
cb
)
{
{
j
=
json
{{
"name"
,
cb
.
m_book_name
},
{
"contacts"
,
cb
.
m_contacts
}};
j
=
json
{{
"name"
,
cb
.
m_book_name
},
{
"contacts"
,
cb
.
m_contacts
}};
}
}
...
@@ -140,28 +140,28 @@ bool operator==(age lhs, age rhs)
...
@@ -140,28 +140,28 @@ bool operator==(age lhs, age rhs)
return
lhs
.
m_val
==
rhs
.
m_val
;
return
lhs
.
m_val
==
rhs
.
m_val
;
}
}
bool
operator
==
(
const
address
&
lhs
,
const
address
&
rhs
)
bool
operator
==
(
const
address
&
lhs
,
const
address
&
rhs
)
{
{
return
lhs
.
m_val
==
rhs
.
m_val
;
return
lhs
.
m_val
==
rhs
.
m_val
;
}
}
bool
operator
==
(
const
name
&
lhs
,
const
name
&
rhs
)
bool
operator
==
(
const
name
&
lhs
,
const
name
&
rhs
)
{
{
return
lhs
.
m_val
==
rhs
.
m_val
;
return
lhs
.
m_val
==
rhs
.
m_val
;
}
}
bool
operator
==
(
const
person
&
lhs
,
const
person
&
rhs
)
bool
operator
==
(
const
person
&
lhs
,
const
person
&
rhs
)
{
{
return
std
::
tie
(
lhs
.
m_name
,
lhs
.
m_age
)
==
std
::
tie
(
rhs
.
m_name
,
rhs
.
m_age
);
return
std
::
tie
(
lhs
.
m_name
,
lhs
.
m_age
)
==
std
::
tie
(
rhs
.
m_name
,
rhs
.
m_age
);
}
}
bool
operator
==
(
const
contact
&
lhs
,
const
contact
&
rhs
)
bool
operator
==
(
const
contact
&
lhs
,
const
contact
&
rhs
)
{
{
return
std
::
tie
(
lhs
.
m_person
,
lhs
.
m_address
)
==
return
std
::
tie
(
lhs
.
m_person
,
lhs
.
m_address
)
==
std
::
tie
(
rhs
.
m_person
,
rhs
.
m_address
);
std
::
tie
(
rhs
.
m_person
,
rhs
.
m_address
);
}
}
bool
operator
==
(
const
contact_book
&
lhs
,
const
contact_book
&
rhs
)
bool
operator
==
(
const
contact_book
&
lhs
,
const
contact_book
&
rhs
)
{
{
return
std
::
tie
(
lhs
.
m_book_name
,
lhs
.
m_contacts
)
==
return
std
::
tie
(
lhs
.
m_book_name
,
lhs
.
m_contacts
)
==
std
::
tie
(
rhs
.
m_book_name
,
rhs
.
m_contacts
);
std
::
tie
(
rhs
.
m_book_name
,
rhs
.
m_contacts
);
...
@@ -172,19 +172,19 @@ bool operator==(const contact_book & lhs, const contact_book & rhs)
...
@@ -172,19 +172,19 @@ bool operator==(const contact_book & lhs, const contact_book & rhs)
namespace
udt
namespace
udt
{
{
template
<
typename
Json
>
template
<
typename
Json
>
void
from_json
(
const
Json
&
j
,
age
&
a
)
void
from_json
(
const
Json
&
j
,
age
&
a
)
{
{
a
.
m_val
=
j
.
template
get
<
int
>
();
a
.
m_val
=
j
.
template
get
<
int
>
();
}
}
template
<
typename
Json
>
template
<
typename
Json
>
void
from_json
(
const
Json
&
j
,
name
&
n
)
void
from_json
(
const
Json
&
j
,
name
&
n
)
{
{
n
.
m_val
=
j
.
template
get
<
std
::
string
>
();
n
.
m_val
=
j
.
template
get
<
std
::
string
>
();
}
}
template
<
typename
Json
>
template
<
typename
Json
>
void
from_json
(
const
Json
&
j
,
country
&
c
)
void
from_json
(
const
Json
&
j
,
country
&
c
)
{
{
const
auto
str
=
j
.
template
get
<
std
::
string
>
();
const
auto
str
=
j
.
template
get
<
std
::
string
>
();
static
const
std
::
map
<
std
::
string
,
country
>
m
=
static
const
std
::
map
<
std
::
string
,
country
>
m
=
...
@@ -200,25 +200,25 @@ void from_json(const Json & j, country& c)
...
@@ -200,25 +200,25 @@ void from_json(const Json & j, country& c)
}
}
template
<
typename
Json
>
template
<
typename
Json
>
void
from_json
(
const
Json
&
j
,
person
&
p
)
void
from_json
(
const
Json
&
j
,
person
&
p
)
{
{
p
.
m_age
=
j
[
"age"
].
template
get
<
age
>
();
p
.
m_age
=
j
[
"age"
].
template
get
<
age
>
();
p
.
m_name
=
j
[
"name"
].
template
get
<
name
>
();
p
.
m_name
=
j
[
"name"
].
template
get
<
name
>
();
p
.
m_country
=
j
[
"country"
].
template
get
<
country
>
();
p
.
m_country
=
j
[
"country"
].
template
get
<
country
>
();
}
}
void
from_json
(
const
nlohmann
::
json
&
j
,
address
&
a
)
void
from_json
(
const
nlohmann
::
json
&
j
,
address
&
a
)
{
{
a
.
m_val
=
j
.
get
<
std
::
string
>
();
a
.
m_val
=
j
.
get
<
std
::
string
>
();
}
}
void
from_json
(
const
nlohmann
::
json
&
j
,
contact
&
c
)
void
from_json
(
const
nlohmann
::
json
&
j
,
contact
&
c
)
{
{
c
.
m_person
=
j
[
"person"
].
get
<
person
>
();
c
.
m_person
=
j
[
"person"
].
get
<
person
>
();
c
.
m_address
=
j
[
"address"
].
get
<
address
>
();
c
.
m_address
=
j
[
"address"
].
get
<
address
>
();
}
}
void
from_json
(
const
nlohmann
::
json
&
j
,
contact_book
&
cb
)
void
from_json
(
const
nlohmann
::
json
&
j
,
contact_book
&
cb
)
{
{
cb
.
m_book_name
=
j
[
"name"
].
get
<
name
>
();
cb
.
m_book_name
=
j
[
"name"
].
get
<
name
>
();
cb
.
m_contacts
=
j
[
"contacts"
].
get
<
std
::
vector
<
contact
>>
();
cb
.
m_contacts
=
j
[
"contacts"
].
get
<
std
::
vector
<
contact
>>
();
...
@@ -297,7 +297,7 @@ namespace nlohmann
...
@@ -297,7 +297,7 @@ namespace nlohmann
template
<
typename
T
>
template
<
typename
T
>
struct
adl_serializer
<
std
::
shared_ptr
<
T
>>
struct
adl_serializer
<
std
::
shared_ptr
<
T
>>
{
{
static
void
to_json
(
json
&
j
,
const
std
::
shared_ptr
<
T
>
&
opt
)
static
void
to_json
(
json
&
j
,
const
std
::
shared_ptr
<
T
>&
opt
)
{
{
if
(
opt
)
if
(
opt
)
{
{
...
@@ -309,7 +309,7 @@ struct adl_serializer<std::shared_ptr<T>>
...
@@ -309,7 +309,7 @@ struct adl_serializer<std::shared_ptr<T>>
}
}
}
}
static
void
from_json
(
const
json
&
j
,
std
::
shared_ptr
<
T
>&
opt
)
static
void
from_json
(
const
json
&
j
,
std
::
shared_ptr
<
T
>&
opt
)
{
{
if
(
j
.
is_null
())
if
(
j
.
is_null
())
{
{
...
@@ -325,12 +325,12 @@ struct adl_serializer<std::shared_ptr<T>>
...
@@ -325,12 +325,12 @@ struct adl_serializer<std::shared_ptr<T>>
template
<>
template
<>
struct
adl_serializer
<
udt
::
legacy_type
>
struct
adl_serializer
<
udt
::
legacy_type
>
{
{
static
void
to_json
(
json
&
j
,
const
udt
::
legacy_type
&
l
)
static
void
to_json
(
json
&
j
,
const
udt
::
legacy_type
&
l
)
{
{
j
=
std
::
stoi
(
l
.
number
);
j
=
std
::
stoi
(
l
.
number
);
}
}
static
void
from_json
(
const
json
&
j
,
udt
::
legacy_type
&
l
)
static
void
from_json
(
const
json
&
j
,
udt
::
legacy_type
&
l
)
{
{
l
.
number
=
std
::
to_string
(
j
.
get
<
int
>
());
l
.
number
=
std
::
to_string
(
j
.
get
<
int
>
());
}
}
...
@@ -395,18 +395,18 @@ template <>
...
@@ -395,18 +395,18 @@ template <>
struct
adl_serializer
<
std
::
vector
<
float
>>
struct
adl_serializer
<
std
::
vector
<
float
>>
{
{
using
type
=
std
::
vector
<
float
>
;
using
type
=
std
::
vector
<
float
>
;
static
void
to_json
(
json
&
j
,
const
type
&
)
static
void
to_json
(
json
&
j
,
const
type
&
)
{
{
j
=
"hijacked!"
;
j
=
"hijacked!"
;
}
}
static
void
from_json
(
const
json
&
,
type
&
opt
)
static
void
from_json
(
const
json
&
,
type
&
opt
)
{
{
opt
=
{
42.0
,
42.0
,
42.0
};
opt
=
{
42.0
,
42.0
,
42.0
};
}
}
// preferred version
// preferred version
static
type
from_json
(
const
json
&
)
static
type
from_json
(
const
json
&
)
{
{
return
{
4.0
,
5.0
,
6.0
};
return
{
4.0
,
5.0
,
6.0
};
}
}
...
@@ -419,7 +419,7 @@ TEST_CASE("even supported types can be specialized", "[udt]")
...
@@ -419,7 +419,7 @@ TEST_CASE("even supported types can be specialized", "[udt]")
CHECK
(
j
.
dump
()
==
R"("hijacked!")"
);
CHECK
(
j
.
dump
()
==
R"("hijacked!")"
);
auto
f
=
j
.
get
<
std
::
vector
<
float
>>
();
auto
f
=
j
.
get
<
std
::
vector
<
float
>>
();
// the single argument from_json method is preferred
// the single argument from_json method is preferred
CHECK
((
f
==
std
::
vector
<
float
>
{
4.0
,
5.0
,
6.0
}));
CHECK
((
f
==
std
::
vector
<
float
>
{
4.0
,
5.0
,
6.0
}));
}
}
namespace
nlohmann
namespace
nlohmann
...
@@ -427,7 +427,7 @@ namespace nlohmann
...
@@ -427,7 +427,7 @@ namespace nlohmann
template
<
typename
T
>
template
<
typename
T
>
struct
adl_serializer
<
std
::
unique_ptr
<
T
>>
struct
adl_serializer
<
std
::
unique_ptr
<
T
>>
{
{
static
void
to_json
(
json
&
j
,
const
std
::
unique_ptr
<
T
>
&
opt
)
static
void
to_json
(
json
&
j
,
const
std
::
unique_ptr
<
T
>&
opt
)
{
{
if
(
opt
)
if
(
opt
)
{
{
...
@@ -440,7 +440,7 @@ struct adl_serializer<std::unique_ptr<T>>
...
@@ -440,7 +440,7 @@ struct adl_serializer<std::unique_ptr<T>>
}
}
// this is the overload needed for non-copyable types,
// this is the overload needed for non-copyable types,
static
std
::
unique_ptr
<
T
>
from_json
(
const
json
&
j
)
static
std
::
unique_ptr
<
T
>
from_json
(
const
json
&
j
)
{
{
if
(
j
.
is_null
())
if
(
j
.
is_null
())
{
{
...
@@ -494,9 +494,9 @@ struct pod_serializer
...
@@ -494,9 +494,9 @@ struct pod_serializer
// use adl for non-pods, or scalar types
// use adl for non-pods, or scalar types
template
<
template
<
typename
Json
,
typename
U
=
T
,
typename
Json
,
typename
U
=
T
,
typename
std
::
enable_if
<
typename
std
::
enable_if
<
not
(
std
::
is_pod
<
U
>::
value
and
std
::
is_class
<
U
>::
value
),
int
>::
type
=
0
>
not
(
std
::
is_pod
<
U
>::
value
and
std
::
is_class
<
U
>::
value
),
int
>::
type
=
0
>
static
void
from_json
(
const
Json
&
j
,
U
&
t
)
static
void
from_json
(
const
Json
&
j
,
U
&
t
)
{
{
using
nlohmann
::
from_json
;
using
nlohmann
::
from_json
;
from_json
(
j
,
t
);
from_json
(
j
,
t
);
...
@@ -506,7 +506,7 @@ struct pod_serializer
...
@@ -506,7 +506,7 @@ struct pod_serializer
template
<
typename
Json
,
typename
U
=
T
,
template
<
typename
Json
,
typename
U
=
T
,
typename
std
::
enable_if
<
typename
std
::
enable_if
<
std
::
is_pod
<
U
>::
value
and
std
::
is_class
<
U
>::
value
,
int
>::
type
=
0
>
std
::
is_pod
<
U
>::
value
and
std
::
is_class
<
U
>::
value
,
int
>::
type
=
0
>
static
void
from_json
(
const
Json
&
j
,
U
&
t
)
static
void
from_json
(
const
Json
&
j
,
U
&
t
)
{
{
std
::
uint64_t
value
;
std
::
uint64_t
value
;
// TODO The following block is no longer relevant in this serializer, make another one that shows the issue
// TODO The following block is no longer relevant in this serializer, make another one that shows the issue
...
@@ -524,15 +524,15 @@ struct pod_serializer
...
@@ -524,15 +524,15 @@ struct pod_serializer
// calling get calls from_json, for now, we cannot do this in custom
// calling get calls from_json, for now, we cannot do this in custom
// serializers
// serializers
nlohmann
::
from_json
(
j
,
value
);
nlohmann
::
from_json
(
j
,
value
);
auto
bytes
=
static_cast
<
char
*>
(
static_cast
<
void
*>
(
&
value
));
auto
bytes
=
static_cast
<
char
*>
(
static_cast
<
void
*>
(
&
value
));
std
::
memcpy
(
&
t
,
bytes
,
sizeof
(
value
));
std
::
memcpy
(
&
t
,
bytes
,
sizeof
(
value
));
}
}
template
<
template
<
typename
Json
,
typename
U
=
T
,
typename
Json
,
typename
U
=
T
,
typename
std
::
enable_if
<
typename
std
::
enable_if
<
not
(
std
::
is_pod
<
U
>::
value
and
std
::
is_class
<
U
>::
value
),
int
>::
type
=
0
>
not
(
std
::
is_pod
<
U
>::
value
and
std
::
is_class
<
U
>::
value
),
int
>::
type
=
0
>
static
void
to_json
(
Json
&
j
,
const
T
&
t
)
static
void
to_json
(
Json
&
j
,
const
T
&
t
)
{
{
using
nlohmann
::
to_json
;
using
nlohmann
::
to_json
;
to_json
(
j
,
t
);
to_json
(
j
,
t
);
...
@@ -541,9 +541,9 @@ struct pod_serializer
...
@@ -541,9 +541,9 @@ struct pod_serializer
template
<
typename
Json
,
typename
U
=
T
,
template
<
typename
Json
,
typename
U
=
T
,
typename
std
::
enable_if
<
typename
std
::
enable_if
<
std
::
is_pod
<
U
>::
value
and
std
::
is_class
<
U
>::
value
,
int
>::
type
=
0
>
std
::
is_pod
<
U
>::
value
and
std
::
is_class
<
U
>::
value
,
int
>::
type
=
0
>
static
void
to_json
(
Json
&
j
,
const
T
&
t
)
noexcept
static
void
to_json
(
Json
&
j
,
const
T
&
t
)
noexcept
{
{
auto
bytes
=
static_cast
<
const
unsigned
char
*>
(
static_cast
<
const
void
*>
(
&
t
));
auto
bytes
=
static_cast
<
const
unsigned
char
*>
(
static_cast
<
const
void
*>
(
&
t
));
std
::
uint64_t
value
=
bytes
[
0
];
std
::
uint64_t
value
=
bytes
[
0
];
for
(
auto
i
=
1
;
i
<
8
;
++
i
)
for
(
auto
i
=
1
;
i
<
8
;
++
i
)
value
|=
std
::
uint64_t
{
bytes
[
i
]}
<<
8
*
i
;
value
|=
std
::
uint64_t
{
bytes
[
i
]}
<<
8
*
i
;
...
@@ -566,13 +566,13 @@ struct non_pod
...
@@ -566,13 +566,13 @@ struct non_pod
};
};
template
<
typename
Json
>
template
<
typename
Json
>
void
to_json
(
Json
&
j
,
const
non_pod
&
np
)
void
to_json
(
Json
&
j
,
const
non_pod
&
np
)
{
{
j
=
np
.
s
;
j
=
np
.
s
;
}
}
template
<
typename
Json
>
template
<
typename
Json
>
void
from_json
(
const
Json
&
j
,
non_pod
&
np
)
void
from_json
(
const
Json
&
j
,
non_pod
&
np
)
{
{
np
.
s
=
j
.
template
get
<
std
::
string
>
();
np
.
s
=
j
.
template
get
<
std
::
string
>
();
}
}
...
@@ -583,7 +583,7 @@ bool operator==(small_pod lhs, small_pod rhs) noexcept
...
@@ -583,7 +583,7 @@ bool operator==(small_pod lhs, small_pod rhs) noexcept
std
::
tie
(
rhs
.
begin
,
rhs
.
middle
,
rhs
.
end
);
std
::
tie
(
rhs
.
begin
,
rhs
.
middle
,
rhs
.
end
);
}
}
bool
operator
==
(
const
non_pod
&
lhs
,
const
non_pod
&
rhs
)
noexcept
bool
operator
==
(
const
non_pod
&
lhs
,
const
non_pod
&
rhs
)
noexcept
{
{
return
lhs
.
s
==
rhs
.
s
;
return
lhs
.
s
==
rhs
.
s
;
}
}
...
@@ -622,13 +622,13 @@ using custom_json = nlohmann::basic_json<std::map, std::vector, std::string, boo
...
@@ -622,13 +622,13 @@ using custom_json = nlohmann::basic_json<std::map, std::vector, std::string, boo
template
<
typename
T
,
typename
>
template
<
typename
T
,
typename
>
struct
another_adl_serializer
struct
another_adl_serializer
{
{
static
void
from_json
(
const
custom_json
&
j
,
T
&
t
)
static
void
from_json
(
const
custom_json
&
j
,
T
&
t
)
{
{
using
nlohmann
::
from_json
;
using
nlohmann
::
from_json
;
from_json
(
j
,
t
);
from_json
(
j
,
t
);
}
}
static
void
to_json
(
custom_json
&
j
,
const
T
&
t
)
static
void
to_json
(
custom_json
&
j
,
const
T
&
t
)
{
{
using
nlohmann
::
to_json
;
using
nlohmann
::
to_json
;
to_json
(
j
,
t
);
to_json
(
j
,
t
);
...
...
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