#JSON parse based on version field

1 messages · Page 1 of 1 (latest)

brisk basalt
#

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

sour lotus
#

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.

brisk basalt
#

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

sour lotus
#

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

brisk basalt
#

oh so std.json will call my jsonParse() kinda like how std.fmt will use a format function if available?

sour lotus
#

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()

brisk basalt
#

ok thanks I'll go look for that stuff!

sour lotus
#

much of that code was copied straight from json.innerParse()

brisk basalt
#

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

sour lotus
#

pretty sure thats a std.json.reader

#

or maybe std.json.Scanner i forget now

brisk basalt
#

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`.

sour lotus
#

ah yes thats why its an anytype

brisk basalt
#

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

sour lotus
#

yeah i agree. get it working first, optimize later.