Giter Site home page Giter Site logo

Comments (5)

ibireme avatar ibireme commented on August 16, 2024 2

Added to master: b63b3e1

from yyjson.

ibireme avatar ibireme commented on August 16, 2024 1

The uint and sint are two different types here, and the function will return false if the types don't match:

yyjson/src/yyjson.h

Lines 7751 to 7755 in f1867b0

/**
Set provided `value` if the JSON Pointer (RFC 6901) exists and is type sint.
Returns true if value at `ptr` exists and is the correct type, otherwise false.
*/
yyjson_api_inline bool yyjson_ptr_get_sint(

I think we should add auto-type conversion to this function, which would return true if the integer can be converted without overflowing.

from yyjson.

chad-earthscope avatar chad-earthscope commented on August 16, 2024

Demonstration code

//cc -Wall -I. yyjson.c yytest.c -o yytest

#include "yyjson.h"
#include "inttypes.h"

int
main ()
{
  const char *jsonstr = "{\"posvalue\": 123, \"negvalue\": -123, \"strvalue\": \"hello\"}";

  yyjson_doc *doc  = NULL;
  yyjson_val *root = NULL;
  int64_t sintvalue = 0;
  uint64_t uintvalue = 0;
  const char *strvalue = NULL;

  doc = yyjson_read (jsonstr, strlen(jsonstr), 0);
  root = yyjson_doc_get_root(doc);

  if (root) {
    printf ("JSON string parsed\n");

    if (yyjson_ptr_get_sint (root, "/posvalue", &sintvalue)) {
      printf ("posvalue(sint): %"PRId64"\n", sintvalue);
    }
    else {
      printf ("posvalue(sint) ERROR\n");
    }

    if (yyjson_ptr_get_uint (root, "/posvalue", &uintvalue)) {
      printf ("posvalue(uint): %"PRIu64"\n", uintvalue);
    }
    else {
      printf ("posvalue(uint) ERROR\n");
    }

    if (yyjson_ptr_get_sint (root, "/negvalue", &sintvalue)) {
      printf ("negvalue(sint): %"PRId64"\n", sintvalue);
    }
    else {
      printf ("negvalue(sint) ERROR\n");
    }

    if (yyjson_ptr_get_str (root, "/strvalue", &strvalue)) {
      printf ("strvalue: %s\n", strvalue);
    }
    else {
      printf ("strvalue ERROR\n");
    }

    yyjson_doc_free(doc);
  }

  return 0;
}

Output:

% cc -Wall -I. yyjson.c yytest.c -o yytest
% ./yytest
JSON string parsed
posvalue(sint) ERROR
posvalue(uint): 123
negvalue(sint): -123
strvalue: hello

from yyjson.

chad-earthscope avatar chad-earthscope commented on August 16, 2024

I note that basic getters of values has a yyjson_get_int() which appears to do the uint or sint functionality. (unfortunately it also truncates to int so the full range of the [u]int64_t is lost). Maybe that approach would work for the ptr versions as well.

from yyjson.

chad-earthscope avatar chad-earthscope commented on August 16, 2024

that's perfect, thanks @ibireme!

from yyjson.

Related Issues (20)

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.