Giter VIP home page Giter VIP logo

Comments (9)

volodimirsmartwave avatar volodimirsmartwave commented on August 24, 2024 1

Oh, thanks for answering so quickly. and the next line, *b = empty_builder;?
are copying the empty structure into b?

from json-build.

lcsmuller avatar lcsmuller commented on August 24, 2024

Hello, its just a "quick hack" to initialize all of jsonb members to zero.

When you create a static or global variable, it is automatically initialized to zero. This:

void
jsonb_init(jsonb *b)
{
  static jsonb empty_builder;
  *b = empty_builder;
...

Is analogous to doing

void
jsonb_init(jsonb *b)
{
  b->pos = 0;
  b->top = NULL;
  for (int i = 0; i < JSONB_MAX_DEPTH; ++i)
    b->stack[i] = 0;
...

Or

void
jsonb_init(jsonb *b)
{
  memset(b, 0, sizeof(jsonb));
...

from json-build.

lcsmuller avatar lcsmuller commented on August 24, 2024

Yup thats precisely whats happening! empty_builder has all of its members set to zero, and thats whats being assigned to *b

from json-build.

volodimirsmartwave avatar volodimirsmartwave commented on August 24, 2024

I was confused about *b. It is like double pointer because b is already pointer.
Thanks for the library, i am trying to use it.
By the way, what do you think about moving "jsonb b" inside of the header and adding to it the pointer to the buffer and the length? In this case jsonb_init will become
void
jsonb_init(jsonb *bb, char * ptr, int len)
{
static jsonb empty_builder;
*bb = empty_builder;

bb->top = bb->stack;
bb->buf_len = len;   // Length of the buffer
bb->buf_ptr = ptr;  // pointer to the buffer

}

and we could avoid passing the buffer and its length to all the functions?

from json-build.

lcsmuller avatar lcsmuller commented on August 24, 2024

Glad to to hear you are finding some use to it :)

Your suggestion is doable but it can be a little awkward if you need to resize the buffer in case its not large enough, you would need to update the jsonb fields everytime that happens. Nevertheless the library should be simple enough for you to wrap your custom logic to it, so you could do something like:

typedef struct myjsonb {
  jsonb builder;
  char *buf;
  size_t bufsize;
} myjsonb;

And then,

jsonbcode
myjsonb_push_string(myjsonb *b, 
                    const char str[],
                    size_t len)
{
  return jsonb_push_string(&b->builder, b->buf, b->bufsize, str, len);
}

from json-build.

volodimirsmartwave avatar volodimirsmartwave commented on August 24, 2024

Thanks for suggestion. With myjsonb_push_string we still pass the buffer and the length to every "push" function. What we initialize the builder with the address of the buffer and its length, and then only pass to the "push" functions the actual needed data?

from json-build.

lcsmuller avatar lcsmuller commented on August 24, 2024

Sorry that was a typo (I fixed it!), it should be passing only the string that will be pushed to the JSON along with its length. buf and bufsize are hidden inside the myjsonb structure.

from json-build.

volodimirsmartwave avatar volodimirsmartwave commented on August 24, 2024

Thanks!

from json-build.

lcsmuller avatar lcsmuller commented on August 24, 2024

No worries, if you got more questions you can open another issue!

from json-build.

Related Issues (3)

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.