Skip to content

Commit

Permalink
Fixed compilation errors, current sticking point is on a template
Browse files Browse the repository at this point in the history
substitution problem involving both array and binary using `std::vector`
Because array is the more general substitution it will match better in
almost all cases.  Need to figure out a reasonable way of constructing a
json with a binary value (thinking static functions, but I need to trace
exactly how json construction hangs together to ensure that I'm doing it
correctly.)
Test failures are due to erroneously constructing an array json value
instead of a binary.
  • Loading branch information
OmnipotentEntity committed Jul 21, 2019
1 parent 2779e5f commit b1c8bdd
Show file tree
Hide file tree
Showing 19 changed files with 939 additions and 68 deletions.
20 changes: 20 additions & 0 deletions include/nlohmann/detail/conversions/to_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ struct external_constructor<value_t::string>
}
};

template<>
struct external_constructor<value_t::binary>
{
template<typename BasicJsonType>
static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
{
j.m_type = value_t::binary;
j.m_value = b;
j.assert_invariant();
}

template<typename BasicJsonType>
static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
{
j.m_type = value_t::binary;
j.m_value = std::move(b);
j.assert_invariant();
}
};

template<>
struct external_constructor<value_t::number_float>
{
Expand Down
12 changes: 6 additions & 6 deletions include/nlohmann/detail/input/binary_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ class binary_reader
@pre len >= 0
@return `true` if the byte array was successfully parsed
*/
template<typename NumberType>
bool get_bson_binary(const NumberType len, binary_t& result)
{
if (JSON_UNLIKELY(len < 0))
Expand Down Expand Up @@ -918,17 +919,16 @@ class binary_reader

case 0x5F: // Binary data (indefinite length)
{
bool success = true;
while (get() != 0xFF)
{
if (JSON_UNLIKELY(not unexpect_eof(format, "binary")))
binary_t chunk;
if (not get_cbor_binary(chunk))
{
success = false;
break;
return false;
}
result.append(static_cast<uint8_t>(current));
result.insert(result.end(), chunk.begin(), chunk.end());
}
return success;
return true;
}

default:
Expand Down
6 changes: 4 additions & 2 deletions include/nlohmann/detail/input/json_sax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class json_sax_dom_parser

bool number_float(number_float_t val, const string_t& /*unused*/)
{
(void)_;
handle_value(val);
return true;
}
Expand Down Expand Up @@ -658,6 +659,7 @@ class json_sax_acceptor
using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
using number_float_t = typename BasicJsonType::number_float_t;
using string_t = typename BasicJsonType::string_t;
using binary_t = typename BasicJsonType::binary_t;

bool null()
{
Expand Down Expand Up @@ -694,7 +696,7 @@ class json_sax_acceptor
return true;
}

bool start_object(std::size_t /*unused*/ = std::size_t(-1))
bool start_object(std::size_t /*unused*/ = std::size_t(-1))
{
return true;
}
Expand All @@ -709,7 +711,7 @@ class json_sax_acceptor
return true;
}

bool start_array(std::size_t /*unused*/ = std::size_t(-1))
bool start_array(std::size_t /*unused*/ = std::size_t(-1))
{
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions include/nlohmann/detail/macro_scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@
template<typename, typename...> class ArrayType, \
class StringType, class BooleanType, class NumberIntegerType, \
class NumberUnsignedType, class NumberFloatType, \
template<typename> class AllocatorType, \
class BinaryType, template<typename> class AllocatorType, \
template<typename, typename = void> class JSONSerializer>

#define NLOHMANN_BASIC_JSON_TPL \
basic_json<ObjectType, ArrayType, StringType, BooleanType, \
NumberIntegerType, NumberUnsignedType, NumberFloatType, \
AllocatorType, JSONSerializer>
BinaryType, AllocatorType, JSONSerializer>
11 changes: 6 additions & 5 deletions include/nlohmann/detail/output/binary_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ template<typename BasicJsonType, typename CharType>
class binary_writer
{
using string_t = typename BasicJsonType::string_t;
using binary_t = typename BasicJsonType::binary_t;

public:
/*!
Expand Down Expand Up @@ -291,7 +292,7 @@ class binary_writer

// step 2: write each element
oa->write_characters(
reinterpret_cast<const uint8_t*>(j.m_value.binary->data()),
reinterpret_cast<const CharType*>(j.m_value.binary->data()),
N);

break;
Expand Down Expand Up @@ -570,7 +571,7 @@ class binary_writer

// step 2: write the byte string
oa->write_characters(
reinterpret_cast<const std::uint8_t*>(j.m_value.binary->data()),
reinterpret_cast<const CharType*>(j.m_value.binary->data()),
N);

break;
Expand Down Expand Up @@ -740,7 +741,7 @@ class binary_writer
}

oa->write_characters(
reinterpret_cast<const std::uint8_t*>(j.m_value.binary->data()),
reinterpret_cast<const CharType*>(j.m_value.binary->data()),
j.m_value.binary->size());

if (not use_count)
Expand Down Expand Up @@ -1005,14 +1006,14 @@ class binary_writer
@brief Writes a BSON element with key @a name and binary value @a value
*/
void write_bson_binary(const string_t& name,
const string_t& value)
const binary_t& value)
{
write_bson_entry_header(name, 0x05);

write_number<std::int32_t, true>(static_cast<std::int32_t>(value.size()));
write_number<std::uint8_t, true>(0x00); // Generic Binary Subtype
oa->write_characters(
reinterpret_cast<const uint8_t*>(value.data()),
reinterpret_cast<const CharType*>(value.data()),
value.size());
}

Expand Down
2 changes: 1 addition & 1 deletion include/nlohmann/detail/output/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class serializer

case value_t::binary:
{
JSON_THROW(type_error::create(317, "cannot serialize binary data to text JSON"))
JSON_THROW(type_error::create(317, "cannot serialize binary data to text JSON"));
}

case value_t::boolean:
Expand Down
2 changes: 1 addition & 1 deletion include/nlohmann/detail/value_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Returns an ordering that is similar to Python:
*/
inline bool operator<(const value_t lhs, const value_t rhs) noexcept
{
static constexpr std::array<std::uint8_t, 8> order = {{
static constexpr std::array<std::uint8_t, 9> order = {{
0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */,
6 /* binary */
Expand Down
12 changes: 6 additions & 6 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ class basic_json

case value_t::binary:
{
array = create<binary_t>();
binary = create<binary_t>();
break;
}

Expand Down Expand Up @@ -1060,16 +1060,16 @@ class basic_json
array = create<array_t>(std::move(value));
}

/// constructor for arrays
/// constructor for binary arrays
json_value(const binary_t& value)
{
array = create<binary_t>(value);
binary = create<binary_t>(value);
}

/// constructor for rvalue arrays
/// constructor for rvalue binary arrays
json_value(binary_t&& value)
{
array = create<binary_t>(std::move(value));
binary = create<binary_t>(std::move(value));
}

void destroy(value_t t) noexcept
Expand Down Expand Up @@ -2683,7 +2683,7 @@ class basic_json
@brief get a value (explicit)
Explicit type conversion between the JSON value and a compatible value
which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
which is [Copynonstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
The value is converted by calling the @ref json_serializer<ValueType>
`from_json()` method.
Expand Down
Loading

0 comments on commit b1c8bdd

Please sign in to comment.