Giter Site home page Giter Site logo

Renaming a key about yyjson HOT 6 CLOSED

ibireme avatar ibireme commented on August 16, 2024
Renaming a key

from yyjson.

Comments (6)

ibireme avatar ibireme commented on August 16, 2024 1

I will release a new version around the end of the year.
But the master branch is stable, and you can use it now.

The function to rename key seems to be rarely used, can you describe how you would use it?

from yyjson.

ibireme avatar ibireme commented on August 16, 2024 1

Added: e98bf96

Note that if the object contains the new_key, there may be duplicate keys after renaming.
e.g. {"a":1,"b":2} after renaming "a" to "b" -> {"b ":1,"b":2}.
You can avoid this by calling yyjson_mut_obj_remove_key() to remove the new_key before renaming.

from yyjson.

ibireme avatar ibireme commented on August 16, 2024

If your key is a constant string, this function is the simplest:

// use iterator to get the key
yyjson_mut_val *key = ...;  
yyjson_mut_set_str(key, "new_str");

If you want yyjson to manage memory for that string, use this function:

// use iterator to get the key
yyjson_mut_val *old_key = ...;
yyjson_mut_val *new_key = yyjson_mut_strncpy(doc, str, str_len);
yyjson_mut_set_strn(old_key, yyjson_mut_get_str(new_key), yyjson_mut_get_len(new_key));

from yyjson.

dan-ryan avatar dan-ryan commented on August 16, 2024

Oh yyjson_mut_set_str is not in the released lib that's why I missed it. Are you doing an update soon?
May I also suggest a method for easily updating keys?
Something like:
yyjson_mut_key_set_str(doc, root, "oldKey", "newKey")

from yyjson.

dan-ryan avatar dan-ryan commented on August 16, 2024

The function to rename key seems to be rarely used, can you describe how you would use it?

Maybe I'm a special case. I use it to fix broken json before feeding it into our system.

from yyjson.

dan-ryan avatar dan-ryan commented on August 16, 2024

Thanks, my testing shows this works great.

from yyjson.

Related Issues (20)

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.