unmarshalling

How to json unmarshalling with custom attribute type in Go

一个人想着一个人 提交于 2021-02-15 07:06:42
问题 In my project, I've defined structures so that get data from JSON. I tried to use json.Unmarshal() function. But it did not work for custom type attribute. There was a structure like this: type TestModel struct { ID NullInt `json:"id"` Name string `json:"name"` } In there, NullInt type was defined with implementations of MarshalJSON() and UnmarshalJSON() functions: // NullInt ... type NullInt struct { Int int Valid bool } // MarshalJSON ... func (ni NullInt) MarshalJSON() ([]byte, error) { if

Is there a way to dynamically unmarshal json base on content? [duplicate]

核能气质少年 提交于 2021-02-08 12:13:48
问题 This question already has answers here : How to parse JSON in golang without unmarshaling twice (3 answers) How to parse a complicated JSON with Go unmarshal? (3 answers) Decoding generic JSON objects to one of many formats (1 answer) How to partially parse JSON using Go? (3 answers) Closed 1 year ago . I have a json format that looks like this { "my_object_list": [ { "meta": {"version": 1}, "my_value": {// Some complex value } } { "meta": {"version": 2}, "my_value": {// Some complex value }

Read and merge two Yaml files

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-07 20:40:25
问题 Assuming we have two yaml files master.yaml someProperty: "someVaue" anotherProperty: "anotherValue" override.yaml someProperty: "overriddenVaue" Is it possible to unmarshall, merge, and then write those changes to a file without having to define a struct for every property in the yaml file? The master file has over 500 properties in it that are not at all important to the service at this point of execution, so ideally I'd be able to just unmarshal into a map, do a merge and write out in yaml

Read and merge two Yaml files

旧城冷巷雨未停 提交于 2021-02-07 20:38:20
问题 Assuming we have two yaml files master.yaml someProperty: "someVaue" anotherProperty: "anotherValue" override.yaml someProperty: "overriddenVaue" Is it possible to unmarshall, merge, and then write those changes to a file without having to define a struct for every property in the yaml file? The master file has over 500 properties in it that are not at all important to the service at this point of execution, so ideally I'd be able to just unmarshal into a map, do a merge and write out in yaml

how to unmarshal json object if object is returning as empty string instead of empty struct

六月ゝ 毕业季﹏ 提交于 2021-02-05 11:37:24
问题 I'm receiving some data as JSON, but if a object is empty, it does not return a empty struct but a empty string instead, and when unmarshaling, it returns an error. So instead of data being {"key":{}} is {"key":""}} , it does not work even using omitempty field Example: https://play.golang.org/p/N1iuWBxuo1C type Store struct { Title string `json:"title,omitempty"` Item item `json:"item,omitempty"` } type item struct { Price float32 `json:"price,omitempty"` Kind string `json:"kind,omitempty"`

Struct is empty after Json unmarshal when accessed by another package

佐手、 提交于 2021-01-28 14:20:44
问题 I'm am using a struct Contact that has a Load() method. The idea is that Load() populates Contact 's fields with data. The data is unmarshalled from json returned by a server. Once loaded, I want to be able to access the data on Contact . I know that the json is unmarshalling correctly because I can print the data to the console within Load() , immediately after it has been unmarshalled. However, this data is not present on Contact once it has been loaded. So when I create a new Contact and

How to merge a json array of json objects to a single json object

若如初见. 提交于 2021-01-28 12:01:50
问题 I need to merge json array of objects such the values of same keys are appended Lets say I have an unknown json array like "jsonarray": [ { "behavior": [ "file", "create_doc_exe" ], "id": 3303511, "platform": "Windows 7 x64 SP1, Adobe Reader 11, Flash 11, Office 2010" }, { "info": [ "sign , 3c4e53e " ], "behavior": [ "sys_folder", "file", "process", "crash" ], "id": 3303, "platform": "Windows XP, Adobe Reader 9.4.0, Flash 10, Office 2007" } ] I want the result to be this way: "jsonarray": {

How to parse JSON arrays with two different data types into a struct in Golang

做~自己de王妃 提交于 2021-01-27 22:13:33
问题 I have JSON that looks like this: { "a": [ [ "12.58861425", 10.52046452 ], [ "12.58861426", 4.1073 ] ] "b": [ [ "12.58861425", 10.52046452 ], [ "12.58861426", 4.1073 ] ] "c": "true" "d": 1234 } I want to unmarshall this into a struct I created: type OrderBook struct { A [][2]float32 `json:"a"` B [][2]float32 `json:"b"` C string `json:"c"` D uint32 `json:"d"` } //Note this won't work because the JSON array contains a string and a float value pair rather than only floats. Normally I would then

JAXB Unmarshalling with dynamic elements [duplicate]

我们两清 提交于 2021-01-27 17:31:29
问题 This question already has answers here : Dealing with poorly designed XML with JAXB (4 answers) Closed 7 years ago . I have an XML as bellow. <hotelRoomDetails> <NightRates> <Night1>67</Night1> <Night2>67.5</Night2> ........ ........ <Night25>65</Night25> </NightRates> ....... </hotelRoomDetails> The element Night1,Night2,Night3 are always dynamic, and this count may vary from 1 to 35 times. Parent tag <NightRates> is always consistent. The element <Night1> , <Night2> .. are not having any

Go json Unmarshaller is not called

不羁岁月 提交于 2020-11-29 10:37:45
问题 I'm fairly new to Golang. And I was looking for a way to do some custom stuff for marshaling and unmarshalling json . I have found the solution to implement Marshaller and Unmarshaller interfaces. Here is my struct with implemented interfaces (I have also implemented Stringer): type Data struct { Foo string `json:"foo"` bar string } func (d Data) MarshalJSON() ([]byte, error) { return []byte("{\"foo\":\"test\",\"bar\":\"data\"}"), nil } func (d Data) String() string { return fmt.Sprintf("Foo: