Menu

#2 Remove item from Object/Array

closed
nobody
None
5
2009-11-25
2009-11-25
No

Would it be possible to supply a function which removes an item from an array or object? I have here the situation that I got a function which gets an json object as parameter. Later on the function creates an own json object where the supplied json object is added. After the function is done it should free its own json object, but not the one given as parameter. However calling cJSON_Delete obviously frees the complete object. To prevent this it is necessarry to remove the json object which had been supplied by parameter from the own json Object. I have done this by directly modifying the json structure which is not so 'clean'. There's one sideeffect: since adding an item to an array/object the cJSON->string entry gets modified by cJSON. But I can live with that.

Discussion

  • Dave Gamble

    Dave Gamble - 2009-11-25

    I've implemented something to help with this case in svn rev 20.

    Here's the svn commit log:
    --------------------------------------------------------------------------------------------------------------
    If you need to add an existing cJSON to a new object, but the existing object
    must not be affected by this, use cJSON_AddItemReferenceTo<Array|Object>.

    This will make a "reference" to the existing object (which is what you really mean to do),
    and allow you to use it with a new object without fear of names being corrupted or things
    being deleted.

    Think of it like a reference, since that's pretty much what it is.
    If you modify the resulting object (i.e. you AddItemReference, then retrieve with GetObjectItem,
    and then start adding/replacing) you'll modify the object you pass in (in other words, this
    doesn't clone everything, since that would probably end up being wasteful of space), however,
    if you add it, and treat it as if it were const, everything will be fine!
    --------------------------------------------------------------------------------------------------------------

    This should resolve your issue in one step - assuming I've not botched anything, just replace your AddItemToObject with AddItemReferenceToObject, and you're set. Check the impl if you have any concerns.

    This seems like a clean solution to your problem.
    That said, you're right and I ought to add a "removeitemfromobject" call. ;)

    Let me know if this does the trick!

    Thanks,

    Dave.

     
  • Dave Gamble

    Dave Gamble - 2009-11-25

    I've added Detach and Remove functionality to svn rev 22.

     
  • Frank Enderle

    Frank Enderle - 2009-11-25

    works perfectly - thanks. one compiler warning you might wanna fix:

    cJSON.c: In function 'cJSON_DetachItemFromArray':
    cJSON.c:461: warning: 'return' with no value, in function returning non-void

    rev 22

    thanks again.

     
  • Frank Enderle

    Frank Enderle - 2009-11-25
    • status: open --> closed
     
  • Dave Gamble

    Dave Gamble - 2009-11-25

    Fixed in rev 23 - thanks for the heads up! :D

     

Log in to post a comment.

MongoDB Logo MongoDB