Skip to content

Commit

Permalink
json: Switch to gason instead of vjson.
Browse files Browse the repository at this point in the history
From the same author.  Most importantly, reads numbers as doubles rather
than as signed ints and floats.  This allows us to actually read 32 bit
unsigned int parameters.

Moved all the native customization to a separate json_reader.cpp.
  • Loading branch information
unknownbrackets committed Apr 29, 2018
1 parent 65ad065 commit 3e9d181
Show file tree
Hide file tree
Showing 24 changed files with 799 additions and 1,092 deletions.
12 changes: 6 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,9 @@ if(USING_GLES2)
find_package(X11)
endif()

add_library(vjson STATIC
ext/native/ext/vjson/json.cpp
ext/native/ext/vjson/json.h
ext/native/ext/vjson/block_allocator.cpp
ext/native/ext/vjson/block_allocator.h
add_library(gason STATIC
ext/native/ext/gason/gason.cpp
ext/native/ext/gason/gason.h
)

add_library(rg_etc1 STATIC
Expand Down Expand Up @@ -972,6 +970,8 @@ add_library(native STATIC
ext/native/input/keycodes.h
ext/native/input/input_state.h
ext/native/input/input_state.cpp
ext/native/json/json_reader.h
ext/native/json/json_reader.cpp
ext/native/json/json_writer.h
ext/native/json/json_writer.cpp
ext/native/math/fast/fast_math.c
Expand Down Expand Up @@ -1060,7 +1060,7 @@ if(ANDROID)
SET(ATOMIC_LIB atomic)
endif()

target_link_libraries(native ${LIBZIP_LIBRARY} ${PNG_LIBRARY} ${ZLIB_LIBRARY} rg_etc1 vjson udis86 ${RT_LIB} ${nativeExtraLibs} ${ATOMIC_LIB})
target_link_libraries(native ${LIBZIP_LIBRARY} ${PNG_LIBRARY} ${ZLIB_LIBRARY} rg_etc1 gason udis86 ${RT_LIB} ${nativeExtraLibs} ${ATOMIC_LIB})
if(TARGET Ext::GLEW)
target_link_libraries(native Ext::GLEW)
endif()
Expand Down
6 changes: 3 additions & 3 deletions Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

#include "base/display.h"
#include "base/NativeApp.h"
#include "ext/vjson/json.h"
#include "file/ini_file.h"
#include "i18n/i18n.h"
#include "json/json_reader.h"
#include "gfx_es2/gpu_features.h"
#include "net/http_client.h"
#include "util/text/parsers.h"
Expand Down Expand Up @@ -1177,13 +1177,13 @@ void Config::DownloadCompletedCallback(http::Download &download) {
}

JsonReader reader(data.c_str(), data.size());
const json_value *root = reader.root();
const JsonGet root = reader.root();
if (!root) {
ERROR_LOG(LOADER, "Failed to parse json");
return;
}

std::string version = root->getString("version", "");
std::string version = root.getString("version", "");

const char *gitVer = PPSSPP_GIT_VERSION;
Version installed(gitVer);
Expand Down
19 changes: 10 additions & 9 deletions UI/RemoteISOScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
#include <condition_variable>

#include "base/timeutil.h"
#include "ext/vjson/json.h"
#include "file/fd_util.h"
#include "i18n/i18n.h"
#include "json/json_reader.h"
#include "net/http_client.h"
#include "net/http_server.h"
#include "net/resolve.h"
Expand Down Expand Up @@ -252,16 +252,19 @@ static bool FindServer(std::string &resultHost, int &resultPort) {
return false;
}

const json_value *entries = reader.root();
if (!entries) {
const JsonValue entries = reader.rootArray();
if (entries.getTag() != JSON_ARRAY) {
return false;
}

std::vector<std::string> servers;
const json_value *entry = entries->first_child;
while (entry && !scanCancelled) {
const char *host = entry->getString("ip", "");
int port = entry->getInt("p", 0);
for (const auto pentry : entries) {
JsonGet entry = pentry->value;
if (scanCancelled)
return false;

const char *host = entry.getString("ip", "");
int port = entry.getInt("p", 0);

char url[1024] = {};
snprintf(url, sizeof(url), "http://%s:%d", host, port);
Expand All @@ -270,8 +273,6 @@ static bool FindServer(std::string &resultHost, int &resultPort) {
if (TryServer(host, port)) {
return true;
}

entry = entry->next_sibling;
}

// None of the local IPs were reachable.
Expand Down
39 changes: 19 additions & 20 deletions UI/Store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <functional>

#include "base/basictypes.h"
#include "ext/vjson/json.h"
#include "json/json_reader.h"

#include "i18n/i18n.h"
#include "ui/screen.h"
Expand Down Expand Up @@ -392,33 +392,32 @@ void StoreScreen::update() {

void StoreScreen::ParseListing(std::string json) {
JsonReader reader(json.c_str(), json.size());
if (!reader.ok()) {
if (!reader.ok() || !reader.root()) {
ELOG("Error parsing JSON from store");
connectionError_ = true;
RecreateViews();
return;
}
json_value *root = reader.root();
const json_value *entries = root->getArray("entries");
const JsonGet root = reader.root();
const JsonNode *entries = root.getArray("entries");
if (entries) {
entries_.clear();
const json_value *game = entries->first_child;
while (game) {
for (const JsonNode *pgame : entries->value) {
JsonGet game = pgame->value;
StoreEntry e;
e.type = ENTRY_PBPZIP;
e.name = GetTranslatedString(game, "name");
e.description = GetTranslatedString(game, "description", "");
e.author = game->getString("author", "?");
e.size = game->getInt("size");
e.downloadURL = game->getString("download-url", "");
e.iconURL = game->getString("icon-url", "");
e.hidden = game->getBool("hidden", false);
const char *file = game->getString("file", 0);
e.author = game.getString("author", "?");
e.size = game.getInt("size");
e.downloadURL = game.getString("download-url", "");
e.iconURL = game.getString("icon-url", "");
e.hidden = game.getBool("hidden", false);
const char *file = game.getString("file", nullptr);
if (!file)
continue;
e.file = file;
entries_.push_back(e);
game = game->next_sibling;
}
}
}
Expand Down Expand Up @@ -541,16 +540,16 @@ std::string StoreScreen::GetStoreJsonURL(std::string storePath) const {
return path;
}

std::string StoreScreen::GetTranslatedString(const json_value *json, std::string key, const char *fallback) const {
const json_value *dict = json->getDict("en_US");
if (dict && json->hasChild(lang_.c_str(), JSON_OBJECT)) {
if (json->getDict(lang_.c_str())->hasChild(key.c_str(), JSON_STRING)) {
dict = json->getDict(lang_.c_str());
std::string StoreScreen::GetTranslatedString(const JsonGet json, std::string key, const char *fallback) const {
JsonGet dict = json.getDict("en_US");
if (dict && json.hasChild(lang_.c_str(), JSON_OBJECT)) {
if (json.getDict(lang_.c_str()).hasChild(key.c_str(), JSON_STRING)) {
dict = json.getDict(lang_.c_str());
}
}
const char *str = 0;
const char *str = nullptr;
if (dict) {
str = dict->getString(key.c_str(), 0);
str = dict.getString(key.c_str(), nullptr);
}
if (str) {
return std::string(str);
Expand Down
4 changes: 2 additions & 2 deletions UI/Store.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
// set game specific settings, etc.
// Uses GameInfoCache heavily to implement the functionality.

struct json_value;
struct JsonGet;
class ProductItemView;

enum EntryType {
Expand Down Expand Up @@ -79,7 +79,7 @@ class StoreScreen : public UIDialogScreenWithBackground {
std::vector<StoreEntry> FilterEntries();

std::string GetStoreJsonURL(std::string storePath) const;
std::string GetTranslatedString(const json_value *json, std::string key, const char *fallback = 0) const;
std::string GetTranslatedString(const JsonGet json, std::string key, const char *fallback = nullptr) const;

std::shared_ptr<http::Download> listing_;
std::shared_ptr<http::Download> image_;
Expand Down
8 changes: 4 additions & 4 deletions UWP/NativeUWP/NativeUWP.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,6 @@
<ClInclude Include="..\..\ext\native\ext\libzip\config.h" />
<ClInclude Include="..\..\ext\native\ext\libzip\zip.h" />
<ClInclude Include="..\..\ext\native\ext\libzip\zipint.h" />
<ClInclude Include="..\..\ext\native\ext\vjson\block_allocator.h" />
<ClInclude Include="..\..\ext\native\ext\vjson\json.h" />
<ClInclude Include="..\..\ext\native\file\chunk_file.h" />
<ClInclude Include="..\..\ext\native\file\fd_util.h" />
<ClInclude Include="..\..\ext\native\file\file_util.h" />
Expand All @@ -338,6 +336,8 @@
<ClInclude Include="..\..\ext\native\file\path.h" />
<ClInclude Include="..\..\ext\native\file\vfs.h" />
<ClInclude Include="..\..\ext\native\file\zip_read.h" />
<ClInclude Include="..\..\ext\native\json\json_reader.h" />
<ClInclude Include="..\..\ext\native\json\json_writer.h" />
<ClInclude Include="..\..\ext\native\gfx\texture_atlas.h" />
<ClInclude Include="..\..\ext\native\gfx_es2\draw_buffer.h" />
<ClInclude Include="..\..\ext\native\gfx_es2\draw_text.h" />
Expand Down Expand Up @@ -1160,15 +1160,15 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='UWP Gold|x64'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="..\..\ext\native\ext\vjson\block_allocator.cpp" />
<ClCompile Include="..\..\ext\native\ext\vjson\json.cpp" />
<ClCompile Include="..\..\ext\native\file\chunk_file.cpp" />
<ClCompile Include="..\..\ext\native\file\fd_util.cpp" />
<ClCompile Include="..\..\ext\native\file\file_util.cpp" />
<ClCompile Include="..\..\ext\native\file\free.cpp" />
<ClCompile Include="..\..\ext\native\file\ini_file.cpp" />
<ClCompile Include="..\..\ext\native\file\path.cpp" />
<ClCompile Include="..\..\ext\native\file\zip_read.cpp" />
<ClCompile Include="..\..\ext\native\json\json_reader.cpp" />
<ClCompile Include="..\..\ext\native\json\json_writer.cpp" />
<ClCompile Include="..\..\ext\native\gfx\texture_atlas.cpp" />
<ClCompile Include="..\..\ext\native\gfx_es2\draw_buffer.cpp" />
<ClCompile Include="..\..\ext\native\gfx_es2\draw_text.cpp" />
Expand Down
15 changes: 0 additions & 15 deletions UWP/NativeUWP/NativeUWP.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
<Filter Include="ext">
<UniqueIdentifier>{2be24387-0b6a-4253-97fa-b8b6f75a8a4c}</UniqueIdentifier>
</Filter>
<Filter Include="ext\vjson">
<UniqueIdentifier>{7fdd3320-a8e0-42ed-b08b-2d86ba2ff414}</UniqueIdentifier>
</Filter>
<Filter Include="ext\libzip">
<UniqueIdentifier>{1a486fc4-bac0-4b33-9139-262272690c74}</UniqueIdentifier>
</Filter>
Expand Down Expand Up @@ -214,12 +211,6 @@
<ClCompile Include="..\..\ext\native\i18n\i18n.cpp">
<Filter>i18n</Filter>
</ClCompile>
<ClCompile Include="..\..\ext\native\ext\vjson\block_allocator.cpp">
<Filter>ext\vjson</Filter>
</ClCompile>
<ClCompile Include="..\..\ext\native\ext\vjson\json.cpp">
<Filter>ext\vjson</Filter>
</ClCompile>
<ClCompile Include="..\..\ext\native\ext\libzip\mkstemp.c">
<Filter>ext\libzip</Filter>
</ClCompile>
Expand Down Expand Up @@ -656,12 +647,6 @@
<ClInclude Include="..\..\ext\native\i18n\i18n.h">
<Filter>i18n</Filter>
</ClInclude>
<ClInclude Include="..\..\ext\native\ext\vjson\block_allocator.h">
<Filter>ext\vjson</Filter>
</ClInclude>
<ClInclude Include="..\..\ext\native\ext\vjson\json.h">
<Filter>ext\vjson</Filter>
</ClInclude>
<ClInclude Include="..\..\ext\native\ext\libzip\config.h">
<Filter>ext\libzip</Filter>
</ClInclude>
Expand Down
4 changes: 2 additions & 2 deletions ext/native/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ LOCAL_SRC_FILES :=\
ext/jpge/jpgd.cpp \
ext/jpge/jpge.cpp \
ext/sha1/sha1.cpp \
ext/vjson/json.cpp \
ext/vjson/block_allocator.cpp \
ext/gason/gason.cpp \
file/fd_util.cpp \
file/chunk_file.cpp \
file/file_util.cpp \
file/free.cpp \
file/path.cpp \
file/ini_file.cpp \
file/zip_read.cpp \
json/json_reader.cpp \
json/json_writer.cpp \
i18n/i18n.cpp \
input/gesture_detector.cpp \
Expand Down
20 changes: 20 additions & 0 deletions ext/native/ext/gason/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2013-2015 Ivan Vashchaev

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.
Loading

0 comments on commit 3e9d181

Please sign in to comment.