Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ion.Timestamp marshal/unmarshal issue - v 1.0.1 #170

Open
szymon-madzielewski opened this issue Oct 30, 2020 · 7 comments
Open

ion.Timestamp marshal/unmarshal issue - v 1.0.1 #170

szymon-madzielewski opened this issue Oct 30, 2020 · 7 comments
Labels
question Further information is requested

Comments

@szymon-madzielewski
Copy link

Hey guys,
I noticed that after marshal/unmarshal Timestamp type we are getting invalid results - a test was done on current version v1.0.1

test case - one fom your interanl test
tmp := ion.NewTimestamp(time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC), ion.TimestampPrecisionSecond, ion.TimezoneUTC)
=> tmp 2010-01-01T00:00:00Z - OK

var jsonData []byte
jsonData, _ = json.Marshal(tmp)
var out = new(helpers.IonTimestamp)
_ = json.Unmarshal(jsonData, &out)
fmt.Println("out", out.String())
==>>
out 0001-01-01T00:00:00Z

@R-maan
Copy link
Contributor

R-maan commented Nov 13, 2020

This Ion library provides its own Marshal/Unmarshal (as Ion can be richer than JSON).
Try using ion.Marshal(temp) and ion.Unmarshal(jsonData, &out)

@szymon-madzielewski
Copy link
Author

Hey, possible, the Issue on our site occurred when we added a cache solution.
Therefore it's very inefficient to add additional rules (and can gro over time) instead of using standard json.Marshal/Unmarshal approach

@joao-reis
Copy link

joao-reis commented Nov 20, 2020

Hey,
I have the same problem to unmarshall the txTime inside of metadata from history queries.
I try to do somethig like this:

type example struct {
	Metadata struct {
		Id      string        `ion:"id"`
		Version int           `ion:"version"`
		TxTime  ion.Timestamp `ion:"txTime"`
	} `ion:"metadata"`
}

The field TxTime is ignored.

@R-maan
Copy link
Contributor

R-maan commented Nov 20, 2020

Hi @joao-reis, based on what I understood form your description, I wrote the following test and it seems to be working fine:

func TestUnmarshalTime(t *testing.T) {
	type MetadataStruct struct {
		Id      string    `ion:"id"`
		Version int       `ion:"version"`
		TxTime  Timestamp `ion:"txTime"`
	}

	type example struct {
		Metadata MetadataStruct `ion:"metadata"`
	}

	ts := NewDateTimestamp(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), TimestampPrecisionYear)
	meta := MetadataStruct{"idValue", 22, ts}
	expectedValue := example{meta}

	t.Run("test", func(t *testing.T) {
		var val example
                // first parameter to unmarshal would be your data
		require.NoError(t, UnmarshalString("{metadata:{id:\"idValue\",txTime:2020T,version:22}}", &val))

		fmt.Println(val)
		assert.Equal(t, expectedValue, val)
	})
}

Can you please clarify how your code is different from this, which results in the mentioned unexpected behaviour?

@joao-reis
Copy link

Hey @Armanbqt ,

So the test works perfectly 👍

The problem here is to show the MetadataStruct in a browser after the unmarshal for example, in this case the field TxTime appear as {} instead of the TxTime.String().
In order to solve this I created a new field and copy the value of TxTime.String() into that new filed to be shown as string.

@R-maan
Copy link
Contributor

R-maan commented Nov 23, 2020

Glad to hear you found a workaround. However, if you provide some code snippet, I might be able to help further.

@therapon therapon added the question Further information is requested label Dec 3, 2020
@jforjava1981
Copy link

jforjava1981 commented Apr 27, 2024

@R-maan, @therapon, @tgregg we are also facing the same issue because ion.Timestamp does not implement MarshalJSON /UnmarshaJSON.

what we are trying to do is this -

  1. ion message is converted to Golang map ( map[string]interface{} ) using
    amazon-ion library's ion.Unmarshal().
  2. this map is then converted to JSON using Golang's native json.Marshal()
    Map created in first step stores values as Golang types exposed by
    amazon-ion library.
    Golang's json Marshaler expects custom types to implement MarshalJSON / UnMarshalJSON
    so that it can convert values of those types to correct JSON.
    Since ion.timestamp type which is stored as value in the map for the ion dates does not implement it ,
    we get default interpretation from Golang Marshaller - {}, instead of JSON date string.

any thoughts ? can we expect implementation of json Marshaller/unmarshaller,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants