forked from maraf/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_parser.cpp
More file actions
148 lines (123 loc) · 4.25 KB
/
json_parser.cpp
File metadata and controls
148 lines (123 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// These are only used by rapidjson/error/en.h to declare the error messages,
// and have to be set to these values before any files are included. They're
// defined here because it's the only place that calls GetParseError().
#undef RAPIDJSON_ERROR_CHARTYPE
#undef RAPIDJSON_ERROR_STRING
#define RAPIDJSON_ERROR_CHARTYPE pal::char_t
#define RAPIDJSON_ERROR_STRING(x) _X(x)
#include <json_parser.h>
#include <rapidjson/error/en.h>
#include "utils.h"
#include <cassert>
#include <cstdint>
namespace {
void get_line_column_from_offset(const char* data, uint64_t size, size_t offset, int *line, int *column)
{
assert(offset <= size);
*line = *column = 1;
for (size_t i = 0; i < offset; i++)
{
(*column)++;
if (data[i] == '\n')
{
(*line)++;
*column = 1;
}
else if (data[i] == '\r' && data[i + 1] == '\n')
{
(*line)++;
*column = 1;
i++; // Discard carriage return
}
}
}
} // empty namespace
bool json_parser_t::parse_raw_data(char* data, int64_t size, const pal::string_t& context)
{
assert(data != nullptr);
constexpr auto flags = rapidjson::ParseFlag::kParseStopWhenDoneFlag | rapidjson::ParseFlag::kParseCommentsFlag;
#ifdef _WIN32
// Can't use in-situ parsing on Windows, as JSON data is encoded in
// UTF-8 and the host expects wide strings. m_document will store
// data in UTF-16 (with pal::char_t as the character type), but it
// has to know that data is encoded in UTF-8 to convert during parsing.
m_document.Parse<flags, rapidjson::UTF8<>>(data);
#else // _WIN32
m_document.ParseInsitu<flags>(data);
#endif // _WIN32
if (m_document.HasParseError())
{
int line, column;
size_t offset = m_document.GetErrorOffset();
get_line_column_from_offset(data, size, offset, &line, &column);
trace::error(_X("A JSON parsing exception occurred in [%s], offset %zu (line %d, column %d): %s"),
context.c_str(), offset, line, column,
rapidjson::GetParseError_En(m_document.GetParseError()));
return false;
}
if (!m_document.IsObject())
{
trace::error(_X("Expected a JSON object in [%s]"), context.c_str());
return false;
}
return true;
}
bool json_parser_t::parse_file(const pal::string_t& path)
{
// This code assumes that the caller has checked that the file `path` exists
// either within the bundle, or as a real file on disk.
assert(m_data == nullptr);
assert(m_bundle_location == nullptr);
if (bundle::info_t::is_single_file_bundle())
{
// Due to in-situ parsing on Linux,
// * The json file is mapped as copy-on-write.
// * The mapping cannot be immediately released, and will be unmapped by the json_parser destructor.
m_data = bundle::info_t::config_t::map(path, m_bundle_location);
if (m_data != nullptr)
{
m_size = (size_t)m_bundle_location->size;
}
}
if (m_data == nullptr)
{
#ifdef _WIN32
// We can't use in-situ parsing on Windows, as JSON data is encoded in
// UTF-8 and the host expects wide strings.
// We do not need copy-on-write, so read-only mapping will be enough.
m_data = (char*)pal::mmap_read(path, &m_size);
#else // _WIN32
m_data = (char*)pal::mmap_copy_on_write(path, &m_size);
#endif // _WIN32
if (m_data == nullptr)
{
trace::error(_X("Cannot use file stream for [%s]: %s"), path.c_str(), pal::strerror(errno).c_str());
return false;
}
}
char *data = m_data;
size_t size = m_size;
// Skip over UTF-8 BOM, if present
if (size >= 3 && data[0] == 0xEF && data[1] == 0xBB && data[1] == 0xBF)
{
size -= 3;
data += 3;
}
return parse_raw_data(data, size, path);
}
json_parser_t::~json_parser_t()
{
if (m_data != nullptr)
{
if (m_bundle_location != nullptr)
{
bundle::info_t::config_t::unmap(m_data, m_bundle_location);
}
else
{
pal::munmap((void*)m_data, m_size);
}
}
}