Skip to content

Commit

Permalink
lejp: E implies float
Browse files Browse the repository at this point in the history
Since eg, 1e-3 is a float without needing a decimal point, let's just
generally take it that anything with the exponent token is a float, ie, 1e3
is also a float despite it can be expressed as an integer.

This seems right also because E is itself not valid in an integer.

#3308
  • Loading branch information
ribes96 authored and lws-team committed Jan 10, 2025
1 parent a74362f commit 1fccae4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/misc/lejp.c
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,8 @@ lejp_parse(struct lejp_ctx *ctx, const unsigned char *json, int len)
}

ctx->buf[ctx->npos] = '\0';
if (ctx->f & LEJP_SEEN_POINT) {
if (ctx->f & (LEJP_SEEN_POINT | LEJP_SEEN_EXP)) {
/* 0.001 or 1E-3 are both floats, take 1E3 as float too */
if (ctx->pst[ctx->pst_sp].callback(ctx,
LEJPCB_VAL_NUM_FLOAT))
goto reject_callback;
Expand Down
19 changes: 19 additions & 0 deletions minimal-examples-lowlevel/api-tests/api-test-lejp/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ static const char * const json_tests[] = {
"{" /* test 12: test 11 but done with LEJP_FLAG_FEAT_OBJECT_INDEXES */
"\"array1\": [[\"a\", \"b\", \"b1\"], [\"c\", \"d\", \"d1\"]],"
"\"array2\": [[\"e\", \"f\", \"f1\"], [\"g\", \"h\", \"h1\"]]"
"}",

"{" /* test 13: float vs int */
"\"a\": 1, \"b\": 1.0, \"c\": 1e-3, \"d\": 1e3"
"}"
};

Expand Down Expand Up @@ -438,6 +442,20 @@ static struct lejp_results {
{ 15, 1, 2, { 1, }, "array2[]", "h1" },
{ 17, 1, 0, { 1, }, "array2[]", "h1" },
{ 3, 1, 0, { 1, }, "array2[]", "h1" },
}, r13[] = {
{ 0, 0, 0, { }, "", "h1" },
{ 2, 0, 0, { }, "", "h1" },
{ 16, 0, 0, { 0, }, "", "h1" },
{ 5, 0, 0, { 0, }, "a", "h1" },
{ 73, 0, 0, { 0, }, "a", "1" },
{ 5, 0, 0, { 1, }, "b", "1" },
{ 74, 0, 0, { 1, }, "b", "1.0" },
{ 5, 0, 0, { 2, }, "c", "1.0" },
{ 74, 0, 0, { 2, }, "c", "1e-3" },
{ 5, 0, 0, { 3, }, "d", "1e-3" },
{ 74, 0, 0, { 3, }, "d", "1e3" },
{ 17, 0, 0, { 3, }, "d", "1e3" },
{ 3, 0, 0, { 3, }, "d", "1e3" },
};

static const char * const tok[] = {
Expand Down Expand Up @@ -469,6 +487,7 @@ struct lejp_results_pkg {
{ r12, LWS_ARRAY_SIZE(r12), tok_test11, LWS_ARRAY_SIZE(tok_test11),
LEJP_FLAG_FEAT_LEADING_WC |
LEJP_FLAG_FEAT_OBJECT_INDEXES },
{ r13, LWS_ARRAY_SIZE(r13), tok, LWS_ARRAY_SIZE(tok), 0 },
};


Expand Down

0 comments on commit 1fccae4

Please sign in to comment.