Giter Club home page Giter Club logo

minijson_reader's People

Contributors

giacomodrago avatar solace-mkourlas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

minijson_reader's Issues

minijson_reader does not distinguish between an unparsable value and a value parsable as zero

minijson_reader provides long and double representations of JSON numbers:

long as_long(): the value as a long integer. This representation is available when type() is Number and the number could be parsed by strtol without overflows, or when the type is Boolean, in which case 1 or 0 are returned for true and false respectively. In all the other cases, 0 is returned.

double as_double(): the value as a double-precision floating-point number. This representation is available when type() is Number and the number could be parsed by strtod without overflows or underflows, or when the type is Boolean, in which case non-zero or 0.0 are returned for true and false respectively. In all the other cases, 0.0 is returned.

The problem is that there’s no way to distinguish between 0 and “this value could not be parsed”.

I know there's a broader rework of number parsing in #4 being discussed, but as a short-term fix we can add long_representation_available and double_representation_available booleans.

Dispatcher example in README does not compile

From README.md:

struct Order
{
    // (comment)
    std::string ticker;
    handler("ticker", [](Order& o, value v) {v.to(o.ticker);}),

Attempting to compile this code causes value::as<std::string> to fail because std::string_view must be used instead. Indeed, Order::ticker is an std::string_view in test/dispatcher.cpp.

parse_object issue

I did an easy test to parse_object() on an json like: "{ "firstName":"John" , "lastName":"Doe" }".
it gives me a exception.
turns out that change the line 144, of minijson_read.hpp from "m_read_offset++" to "++m_read_offset" will solve this ,
or should I just use a json string start with "\n"?

Determine if toplevel content is object or array

If the top-level JSON content that I'm trying to parse can be either an array or an object, how do I tell which one it is? For nested content, I would use value::type(), but for the top-level content I don't have a value to call that on.

build errors on OSX

cmake -S . -B ./cmake-build-minijson_reader_CK-x86_64-Debug/install -G Ninja -D CMAKE_CXX_COMPILER_LAUNCHER=/usr/local/bin/ccache -D CMAKE_EXPORT_COMPILE_COMMANDS=1 -D CMAKE_BUILD_TYPE=Debug -D CMAKE_PREFIX_PATH=/Users/clausklein/Workspace/cpp/minijson_reader_CK/stage -D BUILD_TESTING=1 -D CMAKE_CXX_STANDARD=17 -D CMAKE_INSTALL_PREFIX=/Users/clausklein/Workspace/cpp/minijson_reader_CK/stage
-- The C compiler identification is AppleClang 14.0.0.14000029
-- The CXX compiler identification is AppleClang 14.0.0.14000029
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/usr/bin/gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/usr/bin/g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found GTest: /usr/local/lib/cmake/GTest/GTestConfig.cmake (found version "1.12.1")  
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    BUILD_TESTING


-- Build files have been written to: /Users/clausklein/Workspace/cpp/minijson_reader_CK/cmake-build-minijson_reader_CK-x86_64-Debug/install
+ cmake --build ./cmake-build-minijson_reader_CK-x86_64-Debug/install
[1/6] Building CXX object CMakeFiles/test_value_as.dir/test/value_as.cpp.o
FAILED: CMakeFiles/test_value_as.dir/test/value_as.cpp.o 
/usr/local/bin/ccache /Applications/Xcode.app/Contents/Developer/usr/bin/g++  -I/Users/clausklein/Workspace/cpp/minijson_reader_CK -isystem /usr/local/include -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -std=c++17 -MD -MT CMakeFiles/test_value_as.dir/test/value_as.cpp.o -MF CMakeFiles/test_value_as.dir/test/value_as.cpp.o.d -o CMakeFiles/test_value_as.dir/test/value_as.cpp.o -c /Users/clausklein/Workspace/cpp/minijson_reader_CK/test/value_as.cpp
In file included from /Users/clausklein/Workspace/cpp/minijson_reader_CK/test/value_as.cpp:26:
/Users/clausklein/Workspace/cpp/minijson_reader_CK/minijson_reader.hpp:1102:45: error: call to deleted function 'from_chars'
            const auto [parse_end, error] = std::from_chars(begin, end, result);
                                            ^~~~~~~~~~~~~~~
/Users/clausklein/Workspace/cpp/minijson_reader_CK/test/value_as.cpp:105:16: note: in instantiation of function template specialization 'minijson::value_as_default<double>' requested here
        return value_as_default<T>(v);
               ^
/Users/clausklein/Workspace/cpp/minijson_reader_CK/minijson_reader.hpp:1002:16: note: in instantiation of member function 'minijson::value_as<double>::operator()' requested here
        return value_as<T>()(*this);
               ^
/Users/clausklein/Workspace/cpp/minijson_reader_CK/test/value_as.cpp:135:21: note: in instantiation of function template specialization 'minijson::value::as<double>' requested here
    ASSERT_EQ(42, v.as<double>());
                    ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/charconv:167:6: note: candidate function has been explicitly deleted
void from_chars(const char*, const char*, bool, int = 10) = delete;
     ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/charconv:664:1: note: candidate template ignored: requirement 'is_integral<double>::value' was not satisfied [with _Tp = double]
from_chars(const char* __first, const char* __last, _Tp& __value)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/charconv:671:1: note: candidate function template not viable: requires 4 arguments, but 3 were provided
from_chars(const char* __first, const char* __last, _Tp& __value, int __base)
^
In file included from /Users/clausklein/Workspace/cpp/minijson_reader_CK/test/value_as.cpp:26:
/Users/clausklein/Workspace/cpp/minijson_reader_CK/minijson_reader.hpp:1102:24: error: variable has incomplete type 'const void'
            const auto [parse_end, error] = std::from_chars(begin, end, result);
                       ^
/Users/clausklein/Workspace/cpp/minijson_reader_CK/minijson_reader.hpp:1102:45: error: call to deleted function 'from_chars'
            const auto [parse_end, error] = std::from_chars(begin, end, result);
                                            ^~~~~~~~~~~~~~~
/Users/clausklein/Workspace/cpp/minijson_reader_CK/test/value_as.cpp:105:16: note: in instantiation of function template specialization 'minijson::value_as_default<float>' requested here
        return value_as_default<T>(v);
               ^
/Users/clausklein/Workspace/cpp/minijson_reader_CK/minijson_reader.hpp:1002:16: note: in instantiation of member function 'minijson::value_as<float>::operator()' requested here
        return value_as<T>()(*this);
               ^
/Users/clausklein/Workspace/cpp/minijson_reader_CK/test/value_as.cpp:136:21: note: in instantiation of function template specialization 'minijson::value::as<float>' requested here
    ASSERT_EQ(42, v.as<float>());
                    ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/charconv:167:6: note: candidate function has been explicitly deleted
void from_chars(const char*, const char*, bool, int = 10) = delete;
     ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/charconv:664:1: note: candidate template ignored: requirement 'is_integral<float>::value' was not satisfied [with _Tp = float]
from_chars(const char* __first, const char* __last, _Tp& __value)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/charconv:671:1: note: candidate function template not viable: requires 4 arguments, but 3 were provided
from_chars(const char* __first, const char* __last, _Tp& __value, int __base)
^
In file included from /Users/clausklein/Workspace/cpp/minijson_reader_CK/test/value_as.cpp:26:
/Users/clausklein/Workspace/cpp/minijson_reader_CK/minijson_reader.hpp:1102:24: error: variable has incomplete type 'const void'
            const auto [parse_end, error] = std::from_chars(begin, end, result);
                       ^
4 errors generated.
[2/6] Building CXX object CMakeFiles/test_main.dir/test/main.cpp.o
FAILED: CMakeFiles/test_main.dir/test/main.cpp.o 
# . . .
bash-3.2$ git log -1
commit 632e7221cf4cf3324988b5b7f050d7b9f1bf19b9 (HEAD -> feature/fix-build-on-osx, origin/master, claus/master, master)
Author: giacomodrago <[email protected]>
Date:   Mon Jul 4 22:46:04 2022 +0100

    Update codeql-analysis.yml
bash-3.2$ 

Rework number parsing

In parse_double(...) the value is parsed with std::strtod. This function uses the current locale, for which the separator character can be ',' instead of '.'.

This is an issue for JSON files created in the US and parsed in foreign countries.

One way to fix this problem is to set the thread locale to en-US before calling parse_object(...). However, this creates problems if the same thread displays messages, write files, etc. within parsing handlers.

Another option is to add set_locale() and get_locale() methods to buffer_context_base, a m_locale member initialized to the current locale, and use this in strtod.

C++17 features

Hello,

I would like to check possibility of using minijson_reader for C++14 as QNX compiler I am using supports upto C++14 considering functional safety. Can you please let me know which particular feature of C++17 is being used in the header file? Moreover, I also want to know whether minijson_reader code can be used in ASIL B Application since it's not using any dynamic memory?

Many thanks!

Regards,
Hemal Bavishi

License change request

I wanted to thank you for making such a useful project. I have recently started using this project at work.

I was wondering if you would be willing to change the license to be one of the standard open source licenses? I think the MIT license would most closely match your existing license. Doing so would make it easier for me to use it at work.

Unable to read arrays

I am trying using this project to read a json file which looks like that:

{
	'version': n,
        'capabilities': ['...'],
	'rules': ['rulename'],
	'vbavailable': {
		'rulename': bitnumber
	},
	'vbrequired': n,
	'previousblockhash': 'xxxx',
	'transactions': [{
		'data': 'xxxx',
		'txid': 'xxxx',
		'hash': 'xxxx',
		'depends': [n],
		'fee': n,
		'sigops': n,
		'weight': n,
	}],
	'coinbaseaux': {
		'flags': 'xx'
	},
	'coinbasevalue': n,
	'coinbasetxn': {},
	'target': 'xxxx',
	'mintime': xxx,
	'mutable': ['value'],
	'noncerange': '00000000ffffffff',
	'sigoplimit': n,
	'sizelimit': n,
	'weightlimit': n,
	'curtime': ttt,
	'bits': 'xxxxxxxx',
	'height': n
}

my code looks like this right now:

      buffer_context ctx(chunk.memory, chunk.size);
      try {
        parse_object(ctx, [&](const char* k, value v) {
          dispatch (k)
          <<"result">> [&]() {
            try {
              parse_object(ctx, [&](const char* k, value v) {
                dispatch (k)
                <<"version">> [&]() { result.setVersion(v.as_long()); }
                <<"capabilities">> [&]()
                {
                    parse_array(ctx, [&](value capability)
                    {
                        result.getCapabilities().push_back(capability.as_string());
                    });
                }
...

the value from fields like 'version' or similars are read fine, but with arrays I got no data on them. I try view the data this way:

  cout << "version: " << bt.getVersion() << endl;
  cout << "capabilities (size): " << bt.getCapabilities().size() << endl;

and I always got size 0 for the arrays, even when the json has data. Can I get any hint of how to solve this issue or how to get more data for fing the issue? I try visualize the data putting this code:

cout << capability.as_string() << endl;

inside the parse_array method, and I got the correct values, but still the data is not persisted on the vector used to store the data.

Small compilation errors

In commit 5ea1e3: Line 177 and 187: buffer_context_base should be detail::buffer_context_base
Line 468 and 504: strtol and strtod should be std::strtol and std::strtod respectively.

Rework dispatch

At the moment minijson::dispatch is a bit of a mess, not to mention a sheer abuse of operator overloading.

I would like to do something better (still under the constraint that it shouldn't allocate on the heap): ideally it should be able to (optionally) detect missing and/or unexpected fields and throw.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.