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

change json inplace #25

Open
topolsky opened this issue Jun 16, 2015 · 3 comments
Open

change json inplace #25

topolsky opened this issue Jun 16, 2015 · 3 comments

Comments

@topolsky
Copy link

Hi,

I am trying to implement function to get json, change some part of it and return the changed json.
I thought that its easy.. just parse the json, cast it with AsObject AsArray etc.. change the values inside and then call Stringify on root JSONValue..

But then I found out that the AsObject/AsArray functions are returing const reference which cannot be changed.

I thought that I will use JSONValue::Child() for that but I am not sure if it would work for ::Child(wchar_t*) because there is some const_iterator used..

Is there any way how to achieve inplace change of json easily?
Maybe I overlook/misunderstood something.. sorry for dumb question opening issue..

Also regarding the Child() methods .. why do you not overload subscript [] operator?
Maybe in that way we could just check whats inside JSONValue and go on with [] which would make things easier.. now I have to do the casts all the time which makes it harder to read..

Thanks for your work, I am glad that I do not have to do json stuff myself.. and also your lib compiles on Win XP SP1 which is (sadly) requirement for me :D

@topolsky
Copy link
Author

I changed the const_iterator to iterator in Child and added SetNumber (because I need to change number inside JSONObject), now its sort of inplace.. I have to use JSONValue * everywhere and use only Child() to access "nested" structures.

bool JSONValue::SetNumber(double number)
{
    if (!IsNumber()) {
        return false;
    }
    number_value = number;
    return true;
}

I hope that I am not reinventing the wheel.. but it works for me..

@karelv
Copy link

karelv commented Aug 7, 2017

I have similar issue, I need to add key-value to "JSONObject".
Then I issued a few time "new JSONValue", and a consequence I have memory leakage :-)
When I try to delete the origital jsonvalue pointer, it deletes also memory which is still needed.

I have both variants:

const JSONObject &JSONValue::AsObject() const
{
	return (*object_value);
}

JSONObject &JSONValue::AsObject()
{
	return (*object_value);
}

and your SetNumber function alikes.

@MJPA
Copy link
Owner

MJPA commented Aug 8, 2017

@karelv have you got an example of what you're trying to do?

Running the following through valgrind gives no memory leaks...

int main(int arc, char ** argv) {
  JSONObject obj;
 
  obj[L"foo"] = new JSONValue(L"bar");
  obj[L"pi"] = new JSONValue(3.141);
    
  JSONValue *json = new JSONValue(obj);
  std::wcout << json->Stringify() << std::endl;
    
  delete json;
  return 0;
}

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