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

Convert hjson - object #86

Open
ntnam171 opened this issue Apr 13, 2019 · 4 comments
Open

Convert hjson - object #86

ntnam171 opened this issue Apr 13, 2019 · 4 comments

Comments

@ntnam171
Copy link

Does hjson have fuctions parse hjson to object and convert object to hjson?
e.g.
var myObj = Hjson.Parse(strInput);
var strOutput = Hjson.SerializeObject(myObj);

@dqsully
Copy link
Member

dqsully commented Apr 13, 2019

There should be something like a Hjson.Stringify function, but it really depends on which language you are writing in. Is this in C#?

@ntnam171
Copy link
Author

ntnam171 commented Apr 16, 2019 via email

@dqsully
Copy link
Member

dqsully commented Apr 16, 2019

hjson-cs has a different way of stringifying: https://github.com/hjson/hjson-cs/blob/master/README.md#tostring

myObj.ToString(Stringify.Hjson); // Hjson output
myObj.ToString(Stringify.Formatted); // formatted JSON output
myObj.ToString(Stringify.Plain); // plain JSON output, default
myObj.ToString(); // plain

Or there's also another way using a StringWriter (https://github.com/hjson/hjson-cs/blob/master/sample/Program.cs):

var sw=new StringWriter();
HjsonValue.Save(myObj, sw, new HjsonOptions() { KeepWsc = true });
Console.WriteLine(sw.ToString());

@ntnam171
Copy link
Author

hjson-cs has a different way of stringifying: https://github.com/hjson/hjson-cs/blob/master/README.md#tostring

myObj.ToString(Stringify.Hjson); // Hjson output
myObj.ToString(Stringify.Formatted); // formatted JSON output
myObj.ToString(Stringify.Plain); // plain JSON output, default
myObj.ToString(); // plain

Or there's also another way using a StringWriter (https://github.com/hjson/hjson-cs/blob/master/sample/Program.cs):

var sw=new StringWriter();
HjsonValue.Save(myObj, sw, new HjsonOptions() { KeepWsc = true });
Console.WriteLine(sw.ToString());

Yes. Thanks for your suggestion.
It works when "myObj" is JsonObject.
I think i still convert my custom object to jsonObject.

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

2 participants