#JSON unmarshal to string

13 messages · Page 1 of 1 (latest)

quick girder
#

Which is the best way to unmarshal a json and keep the values "intact". Example
value: 2.0 -> 2.0
value: 2.2 -> 2.2
value: 2 -> 2
value: "true" -> true
value: "22MB" -> 22MB

Currently I see that every number numeric value is becoming a float by default, so value: 2 becomes 2.0 and value: 2.2 -> 2.2000000

stoic bay
#

RawMessage ?

quick girder
#

Thanks @stoic bay , the struct is:

type Config struct {
    Type    string                 `json:"type"`
    Payload map[string]interface{} `json:"payload"`
}

So how would this be modified to incorporate it?

An example json is:

{"type": "reload", "payload": { "shared_buffers": 2.0 }}
worldly inlet
#

may be you can describe each type?

#

instead of map[string]interface{} use actual type

#

then you can add proper types and json should parse them accordingly

quick girder
#

The payload is arbitrary that is the main issue thinkingo Or you mean something else @worldly inlet

worldly inlet
#

i would create a struct for each of the possible payloads

#

it cant be infinite

#

(i hope)

#

then you may check this

quick girder
#

The payload is effectively any Postgresql setting, so yes it will be hard to write a custom one 😅