Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
R
rapidjson
Project
Project
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
rapidjson
Commits
852c2512
Commit
852c2512
authored
Apr 10, 2015
by
Milo Yip
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement parser/generator for JSON Pointer
parent
79e81fe3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
376 additions
and
0 deletions
+376
-0
pointer.h
include/rapidjson/pointer.h
+208
-0
CMakeLists.txt
test/unittest/CMakeLists.txt
+1
-0
pointertest.cpp
test/unittest/pointertest.cpp
+167
-0
No files found.
include/rapidjson/pointer.h
0 → 100644
View file @
852c2512
// Tencent is pleased to support the open source community by making RapidJSON available.
//
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
//
// Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// http://opensource.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#ifndef RAPIDJSON_POINTER_H_
#define RAPIDJSON_POINTER_H_
#include "document.h"
RAPIDJSON_NAMESPACE_BEGIN
template
<
typename
ValueType
,
typename
Allocator
=
CrtAllocator
>
class
GenericPointer
{
public
:
typedef
typename
ValueType
::
EncodingType
EncodingType
;
typedef
typename
EncodingType
::
Ch
Ch
;
struct
Token
{
const
typename
Ch
*
name
;
SizeType
length
;
SizeType
index
;
//!< A valid index if not equal to kInvalidIndex.
};
GenericPointer
(
const
Ch
*
source
,
Allocator
*
allocator
=
0
)
:
allocator_
(
allocator
),
ownAllocator_
(),
nameBuffer_
(),
tokens_
(),
tokenCount_
(),
valid_
(
true
)
{
Parse
(
source
,
internal
::
StrLen
(
source
));
}
GenericPointer
(
const
Ch
*
source
,
size_t
length
,
Allocator
*
allocator
=
0
)
:
allocator_
(
allocator
),
ownAllocator_
(),
nameBuffer_
(),
tokens_
(),
tokenCount_
(),
valid_
(
true
)
{
Parse
(
source
,
length
);
}
GenericPointer
(
const
Token
*
tokens
,
size_t
tokenCount
)
:
:
allocator_
(),
ownAllocator_
(),
nameBuffer_
(),
tokens_
(
tokens
),
tokenCount_
(
tokenCount
),
valid_
(
true
)
{
}
~
GenericPointer
()
{
if
(
nameBuffer_
)
{
Allocator
::
Free
(
nameBuffer_
);
Allocator
::
Free
(
tokens_
);
}
RAPIDJSON_DELETE
(
ownAllocator_
);
}
bool
IsValid
()
const
{
return
valid_
;
}
const
Token
*
GetTokens
()
const
{
return
tokens_
;
}
size_t
GetTokenCount
()
const
{
return
tokenCount_
;
}
template
<
typename
OutputStream
>
void
Stringify
(
OutputStream
&
os
)
const
{
RAPIDJSON_ASSERT
(
IsValid
());
for
(
Token
*
t
=
tokens_
;
t
!=
tokens_
+
tokenCount_
;
++
t
)
{
os
.
Put
(
'/'
);
for
(
size_t
j
=
0
;
j
<
t
->
length
;
j
++
)
{
Ch
c
=
t
->
name
[
j
];
if
(
c
==
'~'
)
{
os
.
Put
(
'~'
);
os
.
Put
(
'0'
);
}
else
if
(
c
==
'/'
)
{
os
.
Put
(
'~'
);
os
.
Put
(
'1'
);
}
else
os
.
Put
(
c
);
}
}
}
ValueType
*
Get
(
ValueType
&
root
)
const
;
const
ValueType
*
Get
(
const
ValueType
&
root
)
const
{
return
Get
(
const_cast
<
ValueType
&>
(
root
));
}
ValueType
*
Get
(
ValueType
&
root
,
const
ValueType
&
defaultValue
)
const
;
const
ValueType
*
Get
(
const
ValueType
&
root
,
const
ValueType
&
defaultValue
)
const
;
// Move semantics, create parents if non-exist
void
Set
(
ValueType
&
root
,
ValueType
&
value
)
const
;
// Create parents if non-exist
void
Swap
(
ValueType
&
root
,
ValueType
&
value
)
const
;
static
const
size_t
kDefaultTokenCapacity
=
4
;
static
const
SizeType
kInvalidIndex
=
~
SizeType
(
0
);
private
:
void
Parse
(
const
Ch
*
source
,
size_t
length
)
{
// Create own allocator if user did not supply.
if
(
!
allocator_
)
ownAllocator_
=
allocator_
=
RAPIDJSON_NEW
(
Allocator
());
// Create a buffer as same size of source
RAPIDJSON_ASSERT
(
nameBuffer_
==
0
);
nameBuffer_
=
(
Ch
*
)
allocator_
->
Malloc
(
length
);
RAPIDJSON_ASSERT
(
tokens_
==
0
);
tokens_
=
(
Token
*
)
allocator_
->
Malloc
(
length
*
sizeof
(
Token
));
// Maximum possible tokens in the source
tokenCount_
=
0
;
Ch
*
name
=
nameBuffer_
;
for
(
size_t
i
=
0
;
i
<
length
;)
{
if
(
source
[
i
++
]
!=
'/'
)
// Consumes '/'
goto
error
;
Token
&
token
=
tokens_
[
tokenCount_
++
];
token
.
name
=
name
;
bool
isNumber
=
true
;
while
(
i
<
length
&&
source
[
i
]
!=
'/'
)
{
Ch
c
=
source
[
i
++
];
// Escaping "~0" -> '~', "~1" -> '/'
if
(
c
==
'~'
)
{
if
(
i
<
length
)
{
c
=
source
[
i
++
];
if
(
c
==
'0'
)
c
=
'~'
;
else
if
(
c
==
'1'
)
c
=
'/'
;
else
goto
error
;
}
else
goto
error
;
}
// First check for index: all of characters are digit
if
(
c
<
'0'
||
c
>
'9'
)
isNumber
=
false
;
*
name
++
=
c
;
}
token
.
length
=
name
-
token
.
name
;
*
name
++
=
'\0'
;
// Null terminator
// Second check for index: more than one digit cannot have leading zero
if
(
isNumber
&&
token
.
length
>
1
&&
token
.
name
[
0
]
==
'0'
)
isNumber
=
false
;
// String to SizeType conversion
SizeType
n
=
0
;
if
(
isNumber
)
{
for
(
size_t
j
=
0
;
j
<
token
.
length
;
j
++
)
{
SizeType
m
=
n
*
10
+
static_cast
<
SizeType
>
(
token
.
name
[
j
]
-
'0'
);
if
(
m
<
n
)
{
// overflow detection
isNumber
=
false
;
break
;
}
n
=
m
;
}
}
token
.
index
=
isNumber
?
n
:
kInvalidIndex
;
}
RAPIDJSON_ASSERT
(
name
<=
nameBuffer_
+
length
);
// Should not overflow buffer
tokens_
=
(
Token
*
)
allocator_
->
Realloc
(
tokens_
,
length
*
sizeof
(
Token
),
tokenCount_
*
sizeof
(
Token
));
// Shrink tokens_
return
;
error
:
Allocator
::
Free
(
nameBuffer_
);
Allocator
::
Free
(
tokens_
);
nameBuffer_
=
0
;
tokens_
=
0
;
tokenCount_
=
0
;
valid_
=
false
;
return
;
}
GenericPointer
(
const
GenericPointer
&
rhs
);
GenericPointer
&
operator
=
(
const
GenericPointer
&
rhs
);
Allocator
*
allocator_
;
Allocator
*
ownAllocator_
;
Ch
*
nameBuffer_
;
Token
*
tokens_
;
size_t
tokenCount_
;
bool
valid_
;
};
typedef
GenericPointer
<
Value
>
Pointer
;
RAPIDJSON_NAMESPACE_END
#endif // RAPIDJSON_POINTER_H_
test/unittest/CMakeLists.txt
View file @
852c2512
...
...
@@ -6,6 +6,7 @@ set(UNITTEST_SOURCES
filestreamtest.cpp
jsoncheckertest.cpp
namespacetest.cpp
pointertest.cpp
readertest.cpp
stringbuffertest.cpp
strtodtest.cpp
...
...
test/unittest/pointertest.cpp
0 → 100644
View file @
852c2512
// Copyright (C) 2011 Milo Yip
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "unittest.h"
#include "rapidjson/pointer.h"
#include "rapidjson/stringbuffer.h"
#include <sstream>
using
namespace
rapidjson
;
static
const
char
cJson
[]
=
"{
\n
"
"
\"
foo
\"
:[
\"
bar
\"
,
\"
baz
\"
],
\n
"
"
\"\"
: 0,
\n
"
"
\"
a/b
\"
: 1,
\n
"
"
\"
c%d
\"
: 2,
\n
"
"
\"
e^f
\"
: 3,
\n
"
"
\"
g|h
\"
: 4,
\n
"
"
\"
i
\\\\
j
\"
: 5,
\n
"
"
\"
k
\\\"
l
\"
: 6,
\n
"
"
\"
\"
: 7,
\n
"
"
\"
m~n
\"
: 8
\n
"
"}"
;
TEST
(
Pointer
,
Parse
)
{
{
Pointer
p
(
""
);
EXPECT_TRUE
(
p
.
IsValid
());
EXPECT_EQ
(
0
,
p
.
GetTokenCount
());
}
{
Pointer
p
(
"/foo"
);
EXPECT_TRUE
(
p
.
IsValid
());
EXPECT_EQ
(
1
,
p
.
GetTokenCount
());
EXPECT_EQ
(
3
,
p
.
GetTokens
()[
0
].
length
);
EXPECT_STREQ
(
"foo"
,
p
.
GetTokens
()[
0
].
name
);
}
{
Pointer
p
(
"/foo/0"
);
EXPECT_TRUE
(
p
.
IsValid
());
EXPECT_EQ
(
2
,
p
.
GetTokenCount
());
EXPECT_EQ
(
3
,
p
.
GetTokens
()[
0
].
length
);
EXPECT_STREQ
(
"foo"
,
p
.
GetTokens
()[
0
].
name
);
EXPECT_EQ
(
1
,
p
.
GetTokens
()[
1
].
length
);
EXPECT_STREQ
(
"0"
,
p
.
GetTokens
()[
1
].
name
);
EXPECT_EQ
(
0
,
p
.
GetTokens
()[
1
].
index
);
}
{
// Unescape ~1
Pointer
p
(
"/a~1b"
);
EXPECT_TRUE
(
p
.
IsValid
());
EXPECT_EQ
(
1
,
p
.
GetTokenCount
());
EXPECT_EQ
(
3
,
p
.
GetTokens
()[
0
].
length
);
EXPECT_STREQ
(
"a/b"
,
p
.
GetTokens
()[
0
].
name
);
}
{
// Unescape ~0
Pointer
p
(
"/m~0n"
);
EXPECT_TRUE
(
p
.
IsValid
());
EXPECT_EQ
(
1
,
p
.
GetTokenCount
());
EXPECT_EQ
(
3
,
p
.
GetTokens
()[
0
].
length
);
EXPECT_STREQ
(
"m~n"
,
p
.
GetTokens
()[
0
].
name
);
}
{
// empty name
Pointer
p
(
"/"
);
EXPECT_TRUE
(
p
.
IsValid
());
EXPECT_EQ
(
1
,
p
.
GetTokenCount
());
EXPECT_EQ
(
0
,
p
.
GetTokens
()[
0
].
length
);
EXPECT_STREQ
(
""
,
p
.
GetTokens
()[
0
].
name
);
}
{
// empty and non-empty name
Pointer
p
(
"//a"
);
EXPECT_TRUE
(
p
.
IsValid
());
EXPECT_EQ
(
2
,
p
.
GetTokenCount
());
EXPECT_EQ
(
0
,
p
.
GetTokens
()[
0
].
length
);
EXPECT_STREQ
(
""
,
p
.
GetTokens
()[
0
].
name
);
EXPECT_EQ
(
1
,
p
.
GetTokens
()[
1
].
length
);
EXPECT_STREQ
(
"a"
,
p
.
GetTokens
()[
1
].
name
);
}
{
// Null characters
Pointer
p
(
"/
\0\0
"
,
3
);
EXPECT_TRUE
(
p
.
IsValid
());
EXPECT_EQ
(
1
,
p
.
GetTokenCount
());
EXPECT_EQ
(
2
,
p
.
GetTokens
()[
0
].
length
);
EXPECT_EQ
(
'\0'
,
p
.
GetTokens
()[
0
].
name
[
0
]);
EXPECT_EQ
(
'\0'
,
p
.
GetTokens
()[
0
].
name
[
1
]);
EXPECT_EQ
(
'\0'
,
p
.
GetTokens
()[
0
].
name
[
2
]);
}
{
// Valid index
Pointer
p
(
"/123"
);
EXPECT_TRUE
(
p
.
IsValid
());
EXPECT_EQ
(
1
,
p
.
GetTokenCount
());
EXPECT_STREQ
(
"123"
,
p
.
GetTokens
()[
0
].
name
);
EXPECT_EQ
(
123
,
p
.
GetTokens
()[
0
].
index
);
}
{
// Invalid index (with leading zero)
Pointer
p
(
"/01"
);
EXPECT_TRUE
(
p
.
IsValid
());
EXPECT_EQ
(
1
,
p
.
GetTokenCount
());
EXPECT_STREQ
(
"01"
,
p
.
GetTokens
()[
0
].
name
);
EXPECT_EQ
(
Pointer
::
kInvalidIndex
,
p
.
GetTokens
()[
0
].
index
);
}
{
// Invalid index (overflow)
Pointer
p
(
"/4294967296"
);
EXPECT_TRUE
(
p
.
IsValid
());
EXPECT_EQ
(
1
,
p
.
GetTokenCount
());
EXPECT_STREQ
(
"4294967296"
,
p
.
GetTokens
()[
0
].
name
);
EXPECT_EQ
(
Pointer
::
kInvalidIndex
,
p
.
GetTokens
()[
0
].
index
);
}
}
TEST
(
Pointer
,
Stringify
)
{
// Test by roundtrip
const
char
*
sources
[]
=
{
""
,
"/foo"
,
"/foo/0"
,
"/"
,
"/a~1b"
,
"/c%d"
,
"/e^f"
,
"/g|h"
,
"/i
\\
j"
,
"/k
\"
l"
,
"/ "
,
"/m~0n"
};
for
(
size_t
i
=
0
;
i
<
sizeof
(
sources
)
/
sizeof
(
sources
[
0
]);
i
++
)
{
Pointer
p
(
sources
[
i
]);
StringBuffer
s
;
p
.
Stringify
(
s
);
EXPECT_STREQ
(
sources
[
i
],
s
.
GetString
());
}
}
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