Skip to content

Get() inside structs #1

Open
Open

Description

@dolmen

Implement Get() traversing into struct{ ... } types as requested by @lestrrat:

  • if the type implements json.Marshaler, call MarshalJSON, convert result to json.RawMessage and then browse into that (requires Get() inside json.RawMessage #2)
  • else follow encoding/json mapping rules using struct tags to map the struct as if it was a map[string]interface{} (an easy (but inefficient) solution could call json.Marshal() and then follow Get() inside json.RawMessage #2)

Activity

  1. self-assigned this
    on Apr 18, 2017
  2. lestrrat commented on Jan 8, 2018

    @lestrrat

    Finally got around to considering using a new package for my json pointer needs.

    Now, what I'd like to do is to apply JSON pointer semantics to a struct, namely a JSON Schema object.

    I want the JSON Schema object to NOT have to be a map[string]interface{}, and I don't want to the object to have to be directly JSON-marshal-able (i.e. it has a MarshalJSON() method, but the struct definition itself is not a one-to-one mapping to the resulting JSON structure)

    I think this issue means "marshal the object to JSON, then apply the logic implemented by jsonptr". Just as a thought, to avoid having to convert the struct into JSON just for this operation, would it make sense to define a type JSONGetter interface { Get(path) (interface{}, error) } or some such, to further delegate the resolution of the JSON pointer to the object itself?

  3. dolmen commented on Sep 25, 2026

    @dolmen
    MemberAuthor

    encoding/json/v2 has now a clear specification of its mapping of structs to JSON:
    https://pkg.go.dev/encoding/json/v2#hdr-JSON_Representation_of_Go_structs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions