#Avoid escaped dynamic json string value when stringifying the outer struct its in

1 messages ยท Page 1 of 1 (latest)

plain socket
#

You can give your struct custom formatter instead of using json.stringify

#

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

icy sage
#

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

plain socket
#

probably need #main instead of #master :P

icy sage
#

got it.. that guide was better ๐Ÿ˜„

#

their github readme should be updated to include some of that info or link to it

plain socket
icy sage
#

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.

plain socket
#

Just to see that I understood right. u.DynJson contains already serialized json right?

icy sage
#

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

plain socket
#

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

icy sage
#

what is this?

#

this a built in?

plain socket
#

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

icy sage
#

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!

plain socket
#

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

icy sage
#

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.