marshal multiple json documents which may have different format versions into the latest struct version

84 views
Skip to first unread message

Chris Burkert

unread,
Apr 28, 2020, 4:52:56 AM4/28/20
to golang-nuts
Dear all,

my application users shall be able to provide multiple json documents (files and urls) which I'd like to marshall into one structure. Additionally these json documents may have different versions. I know how to marshal a document into a version specific struct if I know the format version before (for simplicity of this example I don't handle errors): https://play.golang.org/p/ixVI5CzPqFP

What I would like (in the example the village field was renamed to cities ) is a struct of type ModelV2 with all four values merged in Cities.

Is there a best practice for a backwards compatible behavior which:
  • identifies the json format version of each document
  • skips that document if it is higher than the supported format version in my application
  • merges supported format versions into ONE struct
Of course I have to implement the semantics on my own but how can I approach the topic?

thanks

Manlio Perillo

unread,
Apr 28, 2020, 9:11:29 AM4/28/20
to golang-nuts
You can first unmarshal a struct containing only the Version field.  As an example:


Manlio 
thanks

Chris Burkert

unread,
Apr 29, 2020, 4:31:00 AM4/29/20
to Manlio Perillo, golang-nuts
That sounds like a good plan. I'm going to try that. Thank you Manlio!

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/274e489d-afdb-4ec9-a5b3-26440364c489%40googlegroups.com.

Tom Payne

unread,
May 1, 2020, 9:17:19 PM5/1/20
to golang-nuts
Possibly also useful if you're dealing with JSON documents with different structures:
This will generate the most specific Go struct possible that covers all the example documents that you give it. In your example it will generate:

package main

type T struct {
Cities   []string `json:"Cities,omitempty"`
Version  string   `json:"Version"`
Villages []string `json:"Villages,omitempty"`
}

which is the union of all versions.

If you already know exactly what structures you will receive, then Manilo's approach of first decoding into a struct only the Version field is better though, especially when combined with Go's runtime type switches.

Regards,
Tom


On Wednesday, April 29, 2020 at 9:31:00 AM UTC+1, Chris Burkert wrote:
That sounds like a good plan. I'm going to try that. Thank you Manlio!

Am Di., 28. Apr. 2020 um 15:11 Uhr schrieb Manlio Perillo <manlio...@gmail.com>:
On Tuesday, April 28, 2020 at 10:52:56 AM UTC+2, Chris Burkert wrote:
Dear all,

my application users shall be able to provide multiple json documents (files and urls) which I'd like to marshall into one structure. Additionally these json documents may have different versions. I know how to marshal a document into a version specific struct if I know the format version before (for simplicity of this example I don't handle errors): https://play.golang.org/p/ixVI5CzPqFP

What I would like (in the example the village field was renamed to cities ) is a struct of type ModelV2 with all four values merged in Cities.

Is there a best practice for a backwards compatible behavior which:
  • identifies the json format version of each document
  • skips that document if it is higher than the supported format version in my application
  • merges supported format versions into ONE struct
Of course I have to implement the semantics on my own but how can I approach the topic?


You can first unmarshal a struct containing only the Version field.  As an example:


Manlio 
thanks

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages