You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* feat(parse): add unknown_fields keyword
Add an keyword to typed JSON parsing and parse!
that preserves the current ignore behavior by default while allowing
callers to reject unmatched members with .
This updates the JSON read style plumbing to use StructUtils 2.8's
unknownfield hook, adds regression tests for nested and in-place
parsing, and documents the new option.
* refactor(parse): simplify unknown field policy state
# ERROR: ArgumentError: encountered unknown JSON member "admin" while parsing `Person`
173
+
```
174
+
167
175
This works for nested structs too:
168
176
169
177
```julia
@@ -505,4 +513,4 @@ Let's walk through some notable features of the example above:
505
513
* The `percentages` field is a dictionary with keys of type `Percent`, which is a custom type. The `liftkey` function is defined to convert the JSON string keys to `Percent` types (parses the Float64 manually)
506
514
* The `json_properties` field has a type of `JSONText`, which means the raw JSON will be preserved as a String of the `JSONText` type.
507
515
* The `matrix` field is a `Matrix{Float64}`, so the JSON input array-of-arrays are materialized as such.
508
-
* The `extra_key` field is not defined in the `FrankenStruct` type, so it is ignored and skipped over.
516
+
* The `extra_key` field is not defined in the `FrankenStruct` type, so it is ignored and skipped over by default. Pass `unknown_fields=:error` if you want unknown keys to throw instead.
Copy file name to clipboardExpand all lines: src/parse.jl
+55-20Lines changed: 55 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -18,6 +18,7 @@ Currently supported keyword arguments include:
18
18
* `isroot`: whether this is the root LazyValue encompassing the entire json buffer. If `false` parses only the first JSON value and ignores trailing characters. (default: `true`)
19
19
* `dicttype`: a custom `AbstractDict` type to use instead of `$DEFAULT_OBJECT_TYPE` as the default type for JSON object materialization
20
20
* `null`: a custom value to use for JSON null values (default: `nothing`)
21
+
* `unknown_fields`: controls how unmatched JSON object keys or positional values are handled when parsing into a target type or existing object; supported values are `:ignore` (default) and `:error`
21
22
* `style`: a custom `StructUtils.StructStyle` subtype instance to be used in calls to `StructUtils.make` and `StructUtils.lift`. This allows overriding
22
23
default behaviors for non-owned types.
23
24
@@ -37,7 +38,7 @@ of type `T` will be attempted utilizing machinery and interfaces provided by the
37
38
* If `T` was defined with the `@noarg` macro, an empty instance will be constructed, and field values set as JSON keys match field names
38
39
* If `T` had default field values defined using the `@defaults` or `@kwarg` macros (from StructUtils.jl package), those will be set in the value of `T` unless different values are parsed from the JSON
39
40
* If `T` was defined with the `@nonstruct` macro, the struct will be treated as a primitive type and constructed using the `lift` function rather than from field values
40
-
* JSON keys that don't match field names in `T` will be ignored (skipped over)
41
+
* JSON keys that don't match field names in `T` will be ignored (skipped over) by default; pass `unknown_fields=:error` to reject them
41
42
* If a field in `T` has a `name` fieldtag, the `name` value will be used to match JSON keys instead
42
43
* If `T` or any recursive field type of `T` is abstract, an appropriate `JSON.@choosetype T x -> ...` definition should exist for "choosing" a concrete type at runtime; default type choosing exists for `Union{T, Missing}` and `Union{T, Nothing}` where the JSON value is checked if `null`. If the `Any` type is encountered, the default materialization types will be used (`JSON.Object`, `Vector{Any}`, etc.)
43
44
* For any non-JSON-standard non-aggregate (i.e. non-object, non-array) field type of `T`, a `JSON.lift(::Type{T}, x) = ...` definition can be defined for how to "lift" the default JSON value (String, Number, Bool, `nothing`) to the type `T`; a default lift definition exists, for example, for `JSON.lift(::Type{Missing}, x) = missing` where the standard JSON value for `null` is `nothing` and it can be "lifted" to `missing`
0 commit comments