#Avoid escaped dynamic json string value when stringifying the outer struct its in
1 messages ยท Page 1 of 1 (latest)
the std.json is basic and not full deser framework, if you want latter you might be interested in getty and getty-json which allow more control how fields get (de)serialized
is getty-json maintained, updated, following json schema, etc?
inteesting.. readme only shows how to add it to buid.zig. No details on how to use it.
damn.. tried their info to fetch it.. didnt work
ref not found: master
probably need #main instead of #master :P
https://getty.so/user-guide/tutorial/customization/ the in-band and out-band customization here are what you are gonna find interesting
A (de)serialization framework for Zig
For std.json way, there is std.json.WriteStream https://github.com/ziglang/zig/blob/master/lib/std/json/stringify.zig#L189
With this you can make custom formatter method for your struct
got it.. that guide was better ๐
their github readme should be updated to include some of that info or link to it
Perhaps, its in the parent project getty https://github.com/getty-zig/getty but not in getty-json it seems
dang.. none of that worked. ๐ฆ
guess its still super early days for zig and json
Just want to unescape the escaped characters.. guess there is no built in way to avoid that happening.
Just to see that I understood right. u.DynJson contains already serialized json right?
yah
basically using std.json.stringify() to do that
from a struct
looks like this in the string: "MetaData":"{"Id":"","Name":"zig","Option":"zig","Type":"string","Default":".","Description":"zig option","Meta":""}",
but it has the " " all over it
Ok so, 1. std.json way:
pub fn format(self: @This(), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) std.os.WriteError!void {
const jwriter = std.json.WriteStream(writer, .assumed_correct);
try jwriter.beginObject();
try jwriter.objectField("Name");
try jwriter.write(self.name);
try jwriter.objectField("DynJson");
// NOTE: not using jwriter here, we write raw json
try writer.write(self.DynJson);
try jwriter.endObject();
}
^ Untested, but roughly like that
no that's function you implement for your struct
then you can do std.debug.log("{}", .{my_struct}); for example
it can be different name if you don't want it to be std.fmt compatible
man i have so much to learn lol
most of that is greek to me
oh
this looks a lot like how you would add custom json marshal/unmarshal code in go.
alright..ima look at this more tomorrow.. thank you for your help. appreciate it!
For getty way, actually I don't know if there's straightforward way with getty-json, but I asked the author. It seems like a useful use case anyhow
yah.. I started to load it up.. but its a lot of code to get getty to work.
have to implement all those methods, etc.. seems kinda overkill for this. I am not sure if the std.json is able to handle nested structs or not, but if it is, both ways.. I am not sure I see the benefit of getty.
You would think there is a fairly easy way to avoid escaping strings when stringifying an object that has a string value already. Like just take that string as is maybe.