Switch out jsoncpp for nlohmann json
Add llsdjson.* too!
This commit is contained in:
@@ -10,6 +10,7 @@ include(Linking)
|
||||
include(Boost)
|
||||
include(OpenSSL)
|
||||
include(LLSharedLibs)
|
||||
include(Json)
|
||||
include(GoogleBreakpad)
|
||||
include(Copy3rdPartyLibs)
|
||||
include(ZLIB)
|
||||
@@ -19,6 +20,7 @@ include_directories(
|
||||
${EXPAT_INCLUDE_DIRS}
|
||||
${LLCOMMON_INCLUDE_DIRS}
|
||||
${OPENSSL_INCLUDE_DIRS}
|
||||
${JSON_INCLUDE_DIR}
|
||||
${ZLIB_INCLUDE_DIRS}
|
||||
${BREAKPAD_INCLUDE_DIRECTORIES}
|
||||
)
|
||||
@@ -85,6 +87,7 @@ set(llcommon_SOURCE_FILES
|
||||
llrun.cpp
|
||||
llscopedvolatileaprpool.h
|
||||
llsd.cpp
|
||||
llsdjson.cpp
|
||||
llsdparam.cpp
|
||||
llsdserialize.cpp
|
||||
llsdserialize_xml.cpp
|
||||
@@ -210,6 +213,7 @@ set(llcommon_HEADER_FILES
|
||||
llrun.h
|
||||
llsafehandle.h
|
||||
llsd.h
|
||||
llsdjson.h
|
||||
llsdparam.h
|
||||
llsdserialize.h
|
||||
llsdserialize_xml.h
|
||||
|
||||
126
indra/llcommon/llsdjson.cpp
Normal file
126
indra/llcommon/llsdjson.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||
/**
|
||||
* @file llsdjson.cpp
|
||||
* @brief LLSD flexible data system
|
||||
*
|
||||
* $LicenseInfo:firstyear=2015&license=viewerlgpl$
|
||||
* Second Life Viewer Source Code
|
||||
* Copyright (C) 2015, Linden Research, Inc.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation;
|
||||
* version 2.1 of the License only.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
|
||||
* $/LicenseInfo$
|
||||
*/
|
||||
|
||||
// Must turn on conditional declarations in header file so definitions end up
|
||||
// with proper linkage.
|
||||
#define LLSD_DEBUG_INFO
|
||||
#include "linden_common.h"
|
||||
|
||||
#include "llsdjson.h"
|
||||
|
||||
#include "llerror.h"
|
||||
|
||||
|
||||
//=========================================================================
|
||||
LLSD LlsdFromJson(const nlohmann::json &val)
|
||||
{
|
||||
LLSD result;
|
||||
|
||||
switch (val.type())
|
||||
{
|
||||
default:
|
||||
case nlohmann::json::value_t::null:
|
||||
break;
|
||||
case nlohmann::json::value_t::number_integer:
|
||||
result = LLSD(val.get<LLSD::Integer>());
|
||||
break;
|
||||
case nlohmann::json::value_t::number_unsigned:
|
||||
result = LLSD(val.get<LLSD::Integer>());
|
||||
break;
|
||||
case nlohmann::json::value_t::number_float:
|
||||
result = LLSD(val.get<LLSD::Real>());
|
||||
break;
|
||||
case nlohmann::json::value_t::string:
|
||||
result = LLSD(val.get<LLSD::String>());
|
||||
break;
|
||||
case nlohmann::json::value_t::boolean:
|
||||
result = LLSD(val.get<LLSD::Boolean>());
|
||||
break;
|
||||
case nlohmann::json::value_t::array:
|
||||
result = LLSD::emptyArray();
|
||||
for (const auto &element : val)
|
||||
{
|
||||
result.append(LlsdFromJson(element));
|
||||
}
|
||||
break;
|
||||
case nlohmann::json::value_t::object:
|
||||
result = LLSD::emptyMap();
|
||||
for (auto it = val.cbegin(); it != val.cend(); ++it)
|
||||
{
|
||||
result[it.key()] = LlsdFromJson(it.value());
|
||||
}
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//=========================================================================
|
||||
nlohmann::json LlsdToJson(const LLSD &val)
|
||||
{
|
||||
nlohmann::json result;
|
||||
|
||||
switch (val.type())
|
||||
{
|
||||
case LLSD::TypeUndefined:
|
||||
result = nullptr;
|
||||
break;
|
||||
case LLSD::TypeBoolean:
|
||||
result = val.asBoolean();
|
||||
break;
|
||||
case LLSD::TypeInteger:
|
||||
result = val.asInteger();
|
||||
break;
|
||||
case LLSD::TypeReal:
|
||||
result = val.asReal();
|
||||
break;
|
||||
case LLSD::TypeURI:
|
||||
case LLSD::TypeDate:
|
||||
case LLSD::TypeUUID:
|
||||
case LLSD::TypeString:
|
||||
result = val.asString();
|
||||
break;
|
||||
case LLSD::TypeMap:
|
||||
for (LLSD::map_const_iterator it = val.beginMap(); it != val.endMap(); ++it)
|
||||
{
|
||||
result[it->first] = LlsdToJson(it->second);
|
||||
}
|
||||
break;
|
||||
case LLSD::TypeArray:
|
||||
for (LLSD::array_const_iterator it = val.beginArray(); it != val.endArray(); ++it)
|
||||
{
|
||||
result.push_back(LlsdToJson(*it));
|
||||
}
|
||||
break;
|
||||
case LLSD::TypeBinary:
|
||||
default:
|
||||
LL_ERRS("LlsdToJson") << "Unsupported conversion to JSON from LLSD type (" << val.type() << ")." << LL_ENDL;
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
77
indra/llcommon/llsdjson.h
Normal file
77
indra/llcommon/llsdjson.h
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* @file llsdjson.cpp
|
||||
* @brief LLSD flexible data system
|
||||
*
|
||||
* $LicenseInfo:firstyear=2015&license=viewerlgpl$
|
||||
* Second Life Viewer Source Code
|
||||
* Copyright (C) 2015, Linden Research, Inc.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation;
|
||||
* version 2.1 of the License only.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
|
||||
* $/LicenseInfo$
|
||||
*/
|
||||
|
||||
#ifndef LL_LLSDJSON_H
|
||||
#define LL_LLSDJSON_H
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "stdtypes.h"
|
||||
|
||||
#include "llsd.h"
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
/// Convert a parsed JSON structure into LLSD maintaining member names and
|
||||
/// array indexes.
|
||||
/// JSON/JavaScript types are converted as follows:
|
||||
///
|
||||
/// JSON Type | LLSD Type
|
||||
/// --------------+--------------
|
||||
/// null | undefined
|
||||
/// integer | LLSD::Integer
|
||||
/// unsigned | LLSD::Integer
|
||||
/// real/numeric | LLSD::Real
|
||||
/// string | LLSD::String
|
||||
/// boolean | LLSD::Boolean
|
||||
/// array | LLSD::Array
|
||||
/// object | LLSD::Map
|
||||
///
|
||||
/// For maps and arrays child entries will be converted and added to the structure.
|
||||
/// Order is preserved for an array but not for objects.
|
||||
LLSD LlsdFromJson(const nlohmann::json &val);
|
||||
|
||||
/// Convert an LLSD object into Parsed JSON object maintaining member names and
|
||||
/// array indexs.
|
||||
///
|
||||
/// Types are converted as follows:
|
||||
/// LLSD Type | JSON Type
|
||||
/// --------------+----------------
|
||||
/// TypeUndefined | null
|
||||
/// TypeBoolean | boolean
|
||||
/// TypeInteger | integer
|
||||
/// TypeReal | real/numeric
|
||||
/// TypeString | string
|
||||
/// TypeURI | string
|
||||
/// TypeDate | string
|
||||
/// TypeUUID | string
|
||||
/// TypeMap | object
|
||||
/// TypeArray | array
|
||||
/// TypeBinary | unsupported
|
||||
nlohmann::json LlsdToJson(const LLSD &val);
|
||||
|
||||
#endif // LL_LLSDJSON_H
|
||||
Reference in New Issue
Block a user