Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsmntok_t with smaller size type #239

Open
jcecconi opened this issue Aug 9, 2024 · 3 comments
Open

jsmntok_t with smaller size type #239

jcecconi opened this issue Aug 9, 2024 · 3 comments

Comments

@jcecconi
Copy link

jcecconi commented Aug 9, 2024

Sometimes, in embedded devices like the ESP32, using the 'int' type implies using 32 bits, which is often unnecessary. If you then need to use an array of this struct, it could consume a lot of stack memory. This is why I propose using uint16_t in the following way:

#Iifdef USE_JSMNTYPE_UINT16
typedef struct jsmntok {
jsmntype_t type;
uint16_t start;
uint16_t end;
uint16_t size;
#ifdef JSMN_PARENT_LINKS
uint16_t parent;
#endif
} jsmntok_t;
#endif

@IvanVnucec
Copy link

But that limits the json size. Maybe I'm wrong...

@jcecconi
Copy link
Author

/*

  • Definition of the data type for the indices in the jsmntok_t structure.
  • It can be configured based on the environment using the USE_JSMNTYPE_UINT16 macro.
  • If defined, uint16_t is used to reduce memory consumption in embedded systems.
  • Otherwise, a larger integer type (default: int) is used.
    */
    #ifdef USE_JSMNTYPE_UINT16
    typedef uint16_t jsmn_index_t;
    #else
    typedef int jsmn_index_t; // Or any other default type
    #endif

typedef struct jsmntok {
jsmntype_t type;
jsmn_index_t start;
jsmn_index_t end;
jsmn_index_t size;
#ifdef JSMN_PARENT_LINKS
jsmn_index_t parent;
#endif
} jsmntok_t;

@pt300
Copy link
Collaborator

pt300 commented Mar 19, 2025

Cool idea. Would be good to also ensure JSMN defends itself against overflows. Could be a feature hidden behind a millionth ifdef.
But being able to choose a smaller type for index could be very beneficial if you only expect small JSON strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants