Allow yajl_gen to serialize yajl_val and raw json strings #74

Open
wants to merge 2 commits into
from

Projects

None yet

3 participants

@deepfryed

Hi,

I wanted this feature so i can extract yajl_val snippets from yajl_tree and pass them on to yajl_gen or pass a raw fully formed json snippet to yajl_gen.

I've not seen any tests for yajl_tree and yajl_gen in the codebase atm, but I'd be happy to add some if you're interested in merging this.

Thanks

@stewartbrodie stewartbrodie commented on the diff May 21, 2014
src/yajl_gen.c
+ }
+ status = yajl_gen_array_close(g);
+ if (status != yajl_gen_status_ok) return status;
+ }
+ else if (YAJL_IS_OBJECT(v)) {
+ status = yajl_gen_map_open(g);
+ if (status != yajl_gen_status_ok) return status;
+ for (i = 0; i < YAJL_GET_OBJECT(v)->len; i++) {
+ key = YAJL_GET_OBJECT(v)->keys[i];
+ status = yajl_gen_string(g, (const unsigned char*)key, strlen(key));
+ if (status != yajl_gen_status_ok) return status;
+ status = yajl_gen_val(g, YAJL_GET_OBJECT(v)->values[i]);
+ if (status != yajl_gen_status_ok) return status;
+ }
+ status = yajl_gen_map_close(g);
+ if (status != yajl_gen_status_ok) return status;
@stewartbrodie
stewartbrodie May 21, 2014

This return should not be conditional

@stewartbrodie stewartbrodie commented on the diff May 21, 2014
src/yajl_gen.c
+ return yajl_gen_bool(g, 1);
+ else if (YAJL_IS_FALSE(v))
+ return yajl_gen_bool(g, 0);
+ else if (YAJL_IS_NUMBER(v))
+ return yajl_gen_number(g, v->u.number.r, strlen(v->u.number.r));
+ else if (YAJL_IS_STRING(v))
+ return yajl_gen_string(g, (const unsigned char*)v->u.string, strlen(v->u.string));
+ else if (YAJL_IS_ARRAY(v)) {
+ status = yajl_gen_array_open(g);
+ if (status != yajl_gen_status_ok) return status;
+ for (i = 0; i < YAJL_GET_ARRAY(v)->len; i++) {
+ status = yajl_gen_val(g, YAJL_GET_ARRAY(v)->values[i]);
+ if (status != yajl_gen_status_ok) return status;
+ }
+ status = yajl_gen_array_close(g);
+ if (status != yajl_gen_status_ok) return status;
@stewartbrodie
stewartbrodie May 21, 2014

This return should not be conditional

@stewartbrodie

Hi deepfryed,

I realise that this is a couple of years old now, but I was looking for some code to do exactly this, so I tried incorporating your patch into my build of yajl. Apart from one trivial problem, it's working well for me. I've marked the two places above - as the code stands, processing objects and arrays always returns a failure.

@soulofmachines

stewartbrodie, these should fix the problem

diff --git a/src/yajl_gen.c b/src/yajl_gen.c
index 7024707..8a7e657 100644
--- a/src/yajl_gen.c
+++ b/src/yajl_gen.c
@@ -377,8 +377,7 @@ yajl_gen_val(yajl_gen g, yajl_val v)
             status = yajl_gen_val(g, YAJL_GET_ARRAY(v)->values[i]);
             if (status != yajl_gen_status_ok) return status;
         }
-        status = yajl_gen_array_close(g);
-        if (status != yajl_gen_status_ok) return status;
+        return yajl_gen_array_close(g);
     }
     else if (YAJL_IS_OBJECT(v)) {
         status = yajl_gen_map_open(g);
@@ -390,8 +389,7 @@ yajl_gen_val(yajl_gen g, yajl_val v)
             status = yajl_gen_val(g, YAJL_GET_OBJECT(v)->values[i]);
             if (status != yajl_gen_status_ok) return status;
         }
-        status = yajl_gen_map_close(g);
-        if (status != yajl_gen_status_ok) return status;
+        return yajl_gen_map_close(g);
     }

     /* if control reaches here then something must have gone terribly wrong */
@soulofmachines soulofmachines added a commit to soulofmachines/i3barout that referenced this pull request Mar 30, 2015
@soulofmachines soulofmachines add sorting (lloyd/yajl#74) 96d5faf
@stewartbrodie

Yes, thanks soulofmachines, I agree with that patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment