Skip to content

Commit

Permalink
Merge pull request #2729 from theShmoo/without-io
Browse files Browse the repository at this point in the history
Possibility to use without the dependency to file io and streams to use in intel sgx enclaves
  • Loading branch information
nlohmann committed Jun 26, 2021
2 parents 624f59e + 572343b commit 6471a63
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
11 changes: 9 additions & 2 deletions include/nlohmann/detail/input/input_adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

#include <array> // array
#include <cstddef> // size_t
#include <cstdio> //FILE *
#include <cstring> // strlen
#include <istream> // istream
#include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
#include <memory> // shared_ptr, make_shared, addressof
#include <numeric> // accumulate
#include <string> // string, char_traits
#include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
#include <utility> // pair, declval

#ifndef JSON_NO_IO
#include <cstdio> //FILE *
#include <istream> // istream
#endif // JSON_NO_IO

#include <nlohmann/detail/iterators/iterator_traits.hpp>
#include <nlohmann/detail/macro_scope.hpp>

Expand All @@ -26,6 +29,7 @@ enum class input_format_t { json, cbor, msgpack, ubjson, bson };
// input adapters //
////////////////////

#ifndef JSON_NO_IO
/*!
Input adapter for stdio file access. This adapter read only 1 byte and do not use any
buffer. This adapter is a very low level adapter.
Expand Down Expand Up @@ -117,6 +121,7 @@ class input_stream_adapter
std::istream* is = nullptr;
std::streambuf* sb = nullptr;
};
#endif // JSON_NO_IO

// General-purpose iterator-based adapter. It might not be as fast as
// theoretically possible for some containers, but it is extremely versatile.
Expand Down Expand Up @@ -403,6 +408,7 @@ typename container_input_adapter_factory_impl::container_input_adapter_factory<C
return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
}

#ifndef JSON_NO_IO
// Special cases with fast paths
inline file_input_adapter input_adapter(std::FILE* file)
{
Expand All @@ -418,6 +424,7 @@ inline input_stream_adapter input_adapter(std::istream&& stream)
{
return input_stream_adapter(stream);
}
#endif // JSON_NO_IO

using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));

Expand Down
12 changes: 10 additions & 2 deletions include/nlohmann/detail/output/output_adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

#include <algorithm> // copy
#include <cstddef> // size_t
#include <ios> // streamsize
#include <iterator> // back_inserter
#include <memory> // shared_ptr, make_shared
#include <ostream> // basic_ostream
#include <string> // basic_string
#include <vector> // vector

#ifndef JSON_NO_IO
#include <ios> // streamsize
#include <ostream> // basic_ostream
#endif // JSON_NO_IO

#include <nlohmann/detail/macro_scope.hpp>

namespace nlohmann
Expand Down Expand Up @@ -56,6 +60,7 @@ class output_vector_adapter : public output_adapter_protocol<CharType>
std::vector<CharType>& v;
};

#ifndef JSON_NO_IO
/// output adapter for output streams
template<typename CharType>
class output_stream_adapter : public output_adapter_protocol<CharType>
Expand All @@ -79,6 +84,7 @@ class output_stream_adapter : public output_adapter_protocol<CharType>
private:
std::basic_ostream<CharType>& stream;
};
#endif // JSON_NO_IO

/// output adapter for basic_string
template<typename CharType, typename StringType = std::basic_string<CharType>>
Expand Down Expand Up @@ -111,8 +117,10 @@ class output_adapter
output_adapter(std::vector<CharType>& vec)
: oa(std::make_shared<output_vector_adapter<CharType>>(vec)) {}

#ifndef JSON_NO_IO
output_adapter(std::basic_ostream<CharType>& s)
: oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
#endif // JSON_NO_IO

output_adapter(StringType& s)
: oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
Expand Down
12 changes: 7 additions & 5 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ SOFTWARE.
#include <cstddef> // nullptr_t, ptrdiff_t, size_t
#include <functional> // hash, less
#include <initializer_list> // initializer_list
#include <iosfwd> // istream, ostream
#ifndef JSON_NO_IO
#include <iosfwd> // istream, ostream
#endif // JSON_NO_IO
#include <iterator> // random_access_iterator_tag
#include <memory> // unique_ptr
#include <numeric> // accumulate
Expand Down Expand Up @@ -6659,7 +6661,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @name serialization
/// @{

#ifndef JSON_NO_IO
/*!
@brief serialize to stream
Expand Down Expand Up @@ -6719,7 +6721,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
{
return o << j;
}

#endif // JSON_NO_IO
/// @}


Expand Down Expand Up @@ -6977,7 +6979,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
: detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
}

#ifndef JSON_NO_IO
/*!
@brief deserialize from stream
@deprecated This stream operator is deprecated and will be removed in
Expand Down Expand Up @@ -7022,7 +7024,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
parser(detail::input_adapter(i)).parse(false, j);
return i;
}

#endif // JSON_NO_IO
/// @}

///////////////////////////
Expand Down
35 changes: 26 additions & 9 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ SOFTWARE.
#include <cstddef> // nullptr_t, ptrdiff_t, size_t
#include <functional> // hash, less
#include <initializer_list> // initializer_list
#include <iosfwd> // istream, ostream
#ifndef JSON_NO_IO
#include <iosfwd> // istream, ostream
#endif // JSON_NO_IO
#include <iterator> // random_access_iterator_tag
#include <memory> // unique_ptr
#include <numeric> // accumulate
Expand Down Expand Up @@ -5227,16 +5229,19 @@ std::size_t hash(const BasicJsonType& j)

#include <array> // array
#include <cstddef> // size_t
#include <cstdio> //FILE *
#include <cstring> // strlen
#include <istream> // istream
#include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
#include <memory> // shared_ptr, make_shared, addressof
#include <numeric> // accumulate
#include <string> // string, char_traits
#include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
#include <utility> // pair, declval

#ifndef JSON_NO_IO
#include <cstdio> //FILE *
#include <istream> // istream
#endif // JSON_NO_IO

// #include <nlohmann/detail/iterators/iterator_traits.hpp>

// #include <nlohmann/detail/macro_scope.hpp>
Expand All @@ -5253,6 +5258,7 @@ enum class input_format_t { json, cbor, msgpack, ubjson, bson };
// input adapters //
////////////////////

#ifndef JSON_NO_IO
/*!
Input adapter for stdio file access. This adapter read only 1 byte and do not use any
buffer. This adapter is a very low level adapter.
Expand Down Expand Up @@ -5344,6 +5350,7 @@ class input_stream_adapter
std::istream* is = nullptr;
std::streambuf* sb = nullptr;
};
#endif // JSON_NO_IO

// General-purpose iterator-based adapter. It might not be as fast as
// theoretically possible for some containers, but it is extremely versatile.
Expand Down Expand Up @@ -5630,6 +5637,7 @@ typename container_input_adapter_factory_impl::container_input_adapter_factory<C
return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
}

#ifndef JSON_NO_IO
// Special cases with fast paths
inline file_input_adapter input_adapter(std::FILE* file)
{
Expand All @@ -5645,6 +5653,7 @@ inline input_stream_adapter input_adapter(std::istream&& stream)
{
return input_stream_adapter(stream);
}
#endif // JSON_NO_IO

using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));

Expand Down Expand Up @@ -13119,12 +13128,16 @@ class json_ref

#include <algorithm> // copy
#include <cstddef> // size_t
#include <ios> // streamsize
#include <iterator> // back_inserter
#include <memory> // shared_ptr, make_shared
#include <ostream> // basic_ostream
#include <string> // basic_string
#include <vector> // vector

#ifndef JSON_NO_IO
#include <ios> // streamsize
#include <ostream> // basic_ostream
#endif // JSON_NO_IO

// #include <nlohmann/detail/macro_scope.hpp>


Expand Down Expand Up @@ -13174,6 +13187,7 @@ class output_vector_adapter : public output_adapter_protocol<CharType>
std::vector<CharType>& v;
};

#ifndef JSON_NO_IO
/// output adapter for output streams
template<typename CharType>
class output_stream_adapter : public output_adapter_protocol<CharType>
Expand All @@ -13197,6 +13211,7 @@ class output_stream_adapter : public output_adapter_protocol<CharType>
private:
std::basic_ostream<CharType>& stream;
};
#endif // JSON_NO_IO

/// output adapter for basic_string
template<typename CharType, typename StringType = std::basic_string<CharType>>
Expand Down Expand Up @@ -13229,8 +13244,10 @@ class output_adapter
output_adapter(std::vector<CharType>& vec)
: oa(std::make_shared<output_vector_adapter<CharType>>(vec)) {}

#ifndef JSON_NO_IO
output_adapter(std::basic_ostream<CharType>& s)
: oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
#endif // JSON_NO_IO

output_adapter(StringType& s)
: oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
Expand Down Expand Up @@ -23679,7 +23696,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @name serialization
/// @{

#ifndef JSON_NO_IO
/*!
@brief serialize to stream

Expand Down Expand Up @@ -23739,7 +23756,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
{
return o << j;
}

#endif // JSON_NO_IO
/// @}


Expand Down Expand Up @@ -23997,7 +24014,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
: detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
}

#ifndef JSON_NO_IO
/*!
@brief deserialize from stream
@deprecated This stream operator is deprecated and will be removed in
Expand Down Expand Up @@ -24042,7 +24059,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
parser(detail::input_adapter(i)).parse(false, j);
return i;
}

#endif // JSON_NO_IO
/// @}

///////////////////////////
Expand Down

0 comments on commit 6471a63

Please sign in to comment.