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
965a70e3
Unverified
Commit
965a70e3
authored
Jan 10, 2018
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🔨
optimized output format
parent
fd049676
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
100 additions
and
12 deletions
+100
-12
json.hpp
src/json.hpp
+100
-12
unit-ubjson.cpp
test/src/unit-ubjson.cpp
+0
-0
No files found.
src/json.hpp
View file @
965a70e3
...
@@ -6189,20 +6189,30 @@ class binary_writer
...
@@ -6189,20 +6189,30 @@ class binary_writer
}
}
/*!
/*!
@brief[in] j JSON value to serialize
@param[in] j JSON value to serialize
@param[in] use_count whether to use '#' prefixes (optimized format)
@param[in] use_type whether to use '$' prefixes (optimized format)
@param[in] add_prefix whether prefixes need to be used for this value
*/
*/
void
write_ubjson
(
const
BasicJsonType
&
j
,
const
bool
use_count
=
false
)
void
write_ubjson
(
const
BasicJsonType
&
j
,
const
bool
use_count
=
false
,
const
bool
use_type
=
false
,
const
bool
add_prefix
=
true
)
{
{
switch
(
j
.
type
())
switch
(
j
.
type
())
{
{
case
value_t
:
:
null
:
case
value_t
:
:
null
:
{
{
if
(
add_prefix
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'Z'
));
oa
->
write_character
(
static_cast
<
CharType
>
(
'Z'
));
}
break
;
break
;
}
}
case
value_t
:
:
boolean
:
case
value_t
:
:
boolean
:
{
{
if
(
add_prefix
)
oa
->
write_character
(
j
.
m_value
.
boolean
oa
->
write_character
(
j
.
m_value
.
boolean
?
static_cast
<
CharType
>
(
'T'
)
?
static_cast
<
CharType
>
(
'T'
)
:
static_cast
<
CharType
>
(
'F'
));
:
static_cast
<
CharType
>
(
'F'
));
...
@@ -6211,26 +6221,29 @@ class binary_writer
...
@@ -6211,26 +6221,29 @@ class binary_writer
case
value_t
:
:
number_integer
:
case
value_t
:
:
number_integer
:
{
{
write_number_with_ubjson_prefix
(
j
.
m_value
.
number_integer
);
write_number_with_ubjson_prefix
(
j
.
m_value
.
number_integer
,
add_prefix
);
break
;
break
;
}
}
case
value_t
:
:
number_unsigned
:
case
value_t
:
:
number_unsigned
:
{
{
write_number_with_ubjson_prefix
(
j
.
m_value
.
number_unsigned
);
write_number_with_ubjson_prefix
(
j
.
m_value
.
number_unsigned
,
add_prefix
);
break
;
break
;
}
}
case
value_t
:
:
number_float
:
case
value_t
:
:
number_float
:
{
{
write_number_with_ubjson_prefix
(
j
.
m_value
.
number_float
);
write_number_with_ubjson_prefix
(
j
.
m_value
.
number_float
,
add_prefix
);
break
;
break
;
}
}
case
value_t
:
:
string
:
case
value_t
:
:
string
:
{
{
if
(
add_prefix
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'S'
));
oa
->
write_character
(
static_cast
<
CharType
>
(
'S'
));
write_number_with_ubjson_prefix
(
j
.
m_value
.
string
->
size
());
}
write_number_with_ubjson_prefix
(
j
.
m_value
.
string
->
size
(),
true
);
oa
->
write_characters
(
oa
->
write_characters
(
reinterpret_cast
<
const
CharType
*>
(
j
.
m_value
.
string
->
c_str
()),
reinterpret_cast
<
const
CharType
*>
(
j
.
m_value
.
string
->
c_str
()),
j
.
m_value
.
string
->
size
());
j
.
m_value
.
string
->
size
());
...
@@ -6239,17 +6252,38 @@ class binary_writer
...
@@ -6239,17 +6252,38 @@ class binary_writer
case
value_t
:
:
array
:
case
value_t
:
:
array
:
{
{
if
(
add_prefix
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'['
));
oa
->
write_character
(
static_cast
<
CharType
>
(
'['
));
}
bool
prefix_required
=
true
;
if
(
use_type
and
not
j
.
m_value
.
array
->
empty
())
{
const
char
first_prefix
=
ubjson_prefix
(
j
.
front
());
const
bool
same_prefix
=
std
::
all_of
(
j
.
begin
()
+
1
,
j
.
end
(),
[
this
,
first_prefix
](
const
BasicJsonType
&
v
)
{
return
ubjson_prefix
(
v
)
==
first_prefix
;
});
if
(
same_prefix
)
{
prefix_required
=
false
;
oa
->
write_character
(
static_cast
<
CharType
>
(
'$'
));
oa
->
write_character
(
static_cast
<
CharType
>
(
first_prefix
));
}
}
if
(
use_count
)
if
(
use_count
)
{
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'#'
));
oa
->
write_character
(
static_cast
<
CharType
>
(
'#'
));
write_number_with_ubjson_prefix
(
j
.
m_value
.
array
->
size
());
write_number_with_ubjson_prefix
(
j
.
m_value
.
array
->
size
()
,
true
);
}
}
for
(
const
auto
&
el
:
*
j
.
m_value
.
array
)
for
(
const
auto
&
el
:
*
j
.
m_value
.
array
)
{
{
write_ubjson
(
el
,
use_count
);
write_ubjson
(
el
,
use_count
,
use_type
,
prefix_required
);
}
}
if
(
not
use_count
)
if
(
not
use_count
)
...
@@ -6262,21 +6296,42 @@ class binary_writer
...
@@ -6262,21 +6296,42 @@ class binary_writer
case
value_t
:
:
object
:
case
value_t
:
:
object
:
{
{
if
(
add_prefix
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'{'
));
oa
->
write_character
(
static_cast
<
CharType
>
(
'{'
));
}
bool
prefix_required
=
true
;
if
(
use_type
and
not
j
.
m_value
.
object
->
empty
())
{
const
char
first_prefix
=
ubjson_prefix
(
j
.
front
());
const
bool
same_prefix
=
std
::
all_of
(
j
.
begin
(),
j
.
end
(),
[
this
,
first_prefix
](
const
BasicJsonType
&
v
)
{
return
ubjson_prefix
(
v
)
==
first_prefix
;
});
if
(
same_prefix
)
{
prefix_required
=
false
;
oa
->
write_character
(
static_cast
<
CharType
>
(
'$'
));
oa
->
write_character
(
static_cast
<
CharType
>
(
first_prefix
));
}
}
if
(
use_count
)
if
(
use_count
)
{
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'#'
));
oa
->
write_character
(
static_cast
<
CharType
>
(
'#'
));
write_number_with_ubjson_prefix
(
j
.
m_value
.
object
->
size
());
write_number_with_ubjson_prefix
(
j
.
m_value
.
object
->
size
()
,
true
);
}
}
for
(
const
auto
&
el
:
*
j
.
m_value
.
object
)
for
(
const
auto
&
el
:
*
j
.
m_value
.
object
)
{
{
write_number_with_ubjson_prefix
(
el
.
first
.
size
());
write_number_with_ubjson_prefix
(
el
.
first
.
size
()
,
true
);
oa
->
write_characters
(
oa
->
write_characters
(
reinterpret_cast
<
const
CharType
*>
(
el
.
first
.
c_str
()),
reinterpret_cast
<
const
CharType
*>
(
el
.
first
.
c_str
()),
el
.
first
.
size
());
el
.
first
.
size
());
write_ubjson
(
el
.
second
,
use_count
);
write_ubjson
(
el
.
second
,
use_count
,
use_type
,
prefix_required
);
}
}
if
(
not
use_count
)
if
(
not
use_count
)
...
@@ -6320,38 +6375,56 @@ class binary_writer
...
@@ -6320,38 +6375,56 @@ class binary_writer
}
}
template
<
typename
NumberType
>
template
<
typename
NumberType
>
void
write_number_with_ubjson_prefix
(
const
NumberType
n
)
void
write_number_with_ubjson_prefix
(
const
NumberType
n
,
const
bool
add_prefix
)
{
{
if
(
std
::
is_floating_point
<
NumberType
>::
value
)
if
(
std
::
is_floating_point
<
NumberType
>::
value
)
{
{
if
(
add_prefix
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'D'
));
// float64
oa
->
write_character
(
static_cast
<
CharType
>
(
'D'
));
// float64
}
write_number
(
n
);
write_number
(
n
);
}
}
else
if
(
std
::
is_unsigned
<
NumberType
>::
value
)
else
if
(
std
::
is_unsigned
<
NumberType
>::
value
)
{
{
if
(
n
<=
(
std
::
numeric_limits
<
int8_t
>::
max
)())
if
(
n
<=
(
std
::
numeric_limits
<
int8_t
>::
max
)())
{
{
if
(
add_prefix
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'i'
));
// uint8
oa
->
write_character
(
static_cast
<
CharType
>
(
'i'
));
// uint8
}
write_number
(
static_cast
<
uint8_t
>
(
n
));
write_number
(
static_cast
<
uint8_t
>
(
n
));
}
}
else
if
(
n
<=
(
std
::
numeric_limits
<
uint8_t
>::
max
)())
else
if
(
n
<=
(
std
::
numeric_limits
<
uint8_t
>::
max
)())
{
{
if
(
add_prefix
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'U'
));
// uint8
oa
->
write_character
(
static_cast
<
CharType
>
(
'U'
));
// uint8
}
write_number
(
static_cast
<
uint8_t
>
(
n
));
write_number
(
static_cast
<
uint8_t
>
(
n
));
}
}
else
if
(
n
<=
(
std
::
numeric_limits
<
int16_t
>::
max
)())
else
if
(
n
<=
(
std
::
numeric_limits
<
int16_t
>::
max
)())
{
{
if
(
add_prefix
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'I'
));
// int16
oa
->
write_character
(
static_cast
<
CharType
>
(
'I'
));
// int16
}
write_number
(
static_cast
<
int16_t
>
(
n
));
write_number
(
static_cast
<
int16_t
>
(
n
));
}
}
else
if
(
n
<=
(
std
::
numeric_limits
<
int32_t
>::
max
)())
else
if
(
n
<=
(
std
::
numeric_limits
<
int32_t
>::
max
)())
{
{
if
(
add_prefix
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'l'
));
// int32
oa
->
write_character
(
static_cast
<
CharType
>
(
'l'
));
// int32
}
write_number
(
static_cast
<
int32_t
>
(
n
));
write_number
(
static_cast
<
int32_t
>
(
n
));
}
}
else
if
(
n
<=
(
std
::
numeric_limits
<
int64_t
>::
max
)())
else
if
(
n
<=
(
std
::
numeric_limits
<
int64_t
>::
max
)())
{
{
if
(
add_prefix
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'L'
));
// int64
oa
->
write_character
(
static_cast
<
CharType
>
(
'L'
));
// int64
}
write_number
(
static_cast
<
int64_t
>
(
n
));
write_number
(
static_cast
<
int64_t
>
(
n
));
}
}
else
else
...
@@ -6364,27 +6437,42 @@ class binary_writer
...
@@ -6364,27 +6437,42 @@ class binary_writer
{
{
if
((
std
::
numeric_limits
<
int8_t
>::
min
)()
<=
n
and
n
<=
(
std
::
numeric_limits
<
int8_t
>::
max
)())
if
((
std
::
numeric_limits
<
int8_t
>::
min
)()
<=
n
and
n
<=
(
std
::
numeric_limits
<
int8_t
>::
max
)())
{
{
if
(
add_prefix
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'i'
));
// int8
oa
->
write_character
(
static_cast
<
CharType
>
(
'i'
));
// int8
}
write_number
(
static_cast
<
int8_t
>
(
n
));
write_number
(
static_cast
<
int8_t
>
(
n
));
}
}
else
if
((
std
::
numeric_limits
<
uint8_t
>::
min
)()
<=
n
and
n
<=
(
std
::
numeric_limits
<
uint8_t
>::
max
)())
else
if
((
std
::
numeric_limits
<
uint8_t
>::
min
)()
<=
n
and
n
<=
(
std
::
numeric_limits
<
uint8_t
>::
max
)())
{
{
if
(
add_prefix
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'U'
));
// uint8
oa
->
write_character
(
static_cast
<
CharType
>
(
'U'
));
// uint8
}
write_number
(
static_cast
<
uint8_t
>
(
n
));
write_number
(
static_cast
<
uint8_t
>
(
n
));
}
}
else
if
((
std
::
numeric_limits
<
int16_t
>::
min
)()
<=
n
and
n
<=
(
std
::
numeric_limits
<
int16_t
>::
max
)())
else
if
((
std
::
numeric_limits
<
int16_t
>::
min
)()
<=
n
and
n
<=
(
std
::
numeric_limits
<
int16_t
>::
max
)())
{
{
if
(
add_prefix
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'I'
));
// int16
oa
->
write_character
(
static_cast
<
CharType
>
(
'I'
));
// int16
}
write_number
(
static_cast
<
int16_t
>
(
n
));
write_number
(
static_cast
<
int16_t
>
(
n
));
}
}
else
if
((
std
::
numeric_limits
<
int32_t
>::
min
)()
<=
n
and
n
<=
(
std
::
numeric_limits
<
int32_t
>::
max
)())
else
if
((
std
::
numeric_limits
<
int32_t
>::
min
)()
<=
n
and
n
<=
(
std
::
numeric_limits
<
int32_t
>::
max
)())
{
{
if
(
add_prefix
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'l'
));
// int32
oa
->
write_character
(
static_cast
<
CharType
>
(
'l'
));
// int32
}
write_number
(
static_cast
<
int32_t
>
(
n
));
write_number
(
static_cast
<
int32_t
>
(
n
));
}
}
else
if
((
std
::
numeric_limits
<
int64_t
>::
min
)()
<=
n
and
n
<=
(
std
::
numeric_limits
<
int64_t
>::
max
)())
else
if
((
std
::
numeric_limits
<
int64_t
>::
min
)()
<=
n
and
n
<=
(
std
::
numeric_limits
<
int64_t
>::
max
)())
{
{
if
(
add_prefix
)
{
oa
->
write_character
(
static_cast
<
CharType
>
(
'L'
));
// int64
oa
->
write_character
(
static_cast
<
CharType
>
(
'L'
));
// int64
}
write_number
(
static_cast
<
int64_t
>
(
n
));
write_number
(
static_cast
<
int64_t
>
(
n
));
}
}
else
else
...
...
test/src/unit-ubjson.cpp
View file @
965a70e3
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