I want to have a version field that tells my code what version of the format it is using (in case I need to change it in the future), and then turn the rest of the fields into an std.json.Parsed(MyJSONConfigOld) , std.json.Parsed(MyJSONConfigNew) etc. I know I can just turn it all into a std.json.Value but I feel like it shouldn't have to all be dynamic just because of 1 field
#JSON parse based on version field
1 messages · Page 1 of 1 (latest)
i think maybe the best thing would be to have conversion methods in your various Config structs which might prune and deinit() any unused fields. and for old -> new, maybe set default values for missing fields.
maybe another less efficient way would be to json.stringify() the old and re-parse with some config such as ignore_missing_fields.
for now I don't really care about converting between them. all I want is to be able to parse the config json differently depending on the version number (maybe by putting it in a union or smth) so I can treat it differently
after thinking about it a little bit this is kinda what I want:
{
"version": 0,
"author": "bob"
}```
and
```js
{
"version": 1,
"author_email": "[email protected]",
"hash": "b4669ad3"
}```
both get put into a `std.json.Parsed(MyJSONConfig)`, where `MyJSONConfig` looks something like:
```rust
const MyJSONConfig = union(enum) {
old: struct { author: []const u8 },
new: struct {
author_email: []const u8,
hash: []const u8,
},
}```
but idk how to configure std.json to do something like that
or if its even possible
oic. in that case it might make sense to add a jsonParse() method to the MyJsonConfig enum and parse into one of those. then you can do custom parsing to detect the version and call back into std.json.innerParse(JsonConfigOld) or whatever depending on the version
jsonParse() needs to be pub and have the right signature
oh so std.json will call my jsonParse() kinda like how std.fmt will use a format function if available?
i think you can find examples in std.json somewhere. maybe look at innerParse() for calls to jsonParse()
yes it allows you to make custom parsers by adding jsonParse()
indeed very similar to fmt.format()
ok thanks I'll go look for that stuff!
here's an example w/ custom jsonParse() i made from a couple days ago https://zigbin.io/318260
much of that code was copied straight from json.innerParse()
what does source need to provide?
from first glance I see a next method but idk what thats supposed to return
now I understand the people that want comptime interfaces lol
ok found what its supposed to be
/// `scanner_or_reader` must be either a `*std.json.Scanner` with complete input or a `*std.json.Reader`.
ah yes thats why its an anytype
this is taken from doc comment of parseFromTokenSource which passes scanner_or_reader directly to source
oh wait
parseFromValue exists
which kinda does what I want?
I mean I'm still putting the whole thing into a Value then taking the whole thing out but that seems like a future me problem
bc its only gonna run once at startup so who cares how slow it is
yeah i agree. get it working first, optimize later.