<FromGitter>
<naqvis> use where? and what's your intention?
<FromGitter>
<naqvis> do you need to parse date time?
<FromGitter>
<Dan-Do> Yes, I want to parse date from a string, I know that we can use Time.parse, but curious how to use the above function :)
<FromGitter>
<Dan-Do> Can it just be simple as `Time.new("{foo:'2021-03-11'")`?
<FromGitter>
<oprypin:matrix.org> @dan: are you using json or not?
<FromGitter>
<Dan-Do> yeap, the string is from a unknown schema json
<FromGitter>
<oprypin:matrix.org> @dan: do you at this point have a string with just the time expr, or is it still JSON?
<FromGitter>
<Dan-Do> a string
<FromGitter>
<oprypin:matrix.org> then you don't need a json-specific helper. why would you want to put it back into JSON only to tell it to unwrap it again
<FromGitter>
<oprypin:matrix.org> don't use Time.new, copy what's inside its implementation directly
<FromGitter>
<Dan-Do> ah, I just curious how to use that method :)
<FromGitter>
<Dan-Do> I don't see any `Time.new` there
<FromGitter>
<oprypin:matrix.org> a json::serializable object having a Time field will be parsed from the string as part of a higher-level json parsing
<FromGitter>
<Dan-Do> okay, as I understand, the `Time.new(pull : JSON::PullParser)` can be only used in a `from_json`, right?
<FromGitter>
<Dan-Do> we cannot call it directly as `Time.parse`
<FromGitter>
<naqvis> `Time::Format::ISO_8601_DATE_TIME.parse(string)` you can use in this way
<FromGitter>
<naqvis> where `string` is string containing the specific formatted datetime
<FromGitter>
<Dan-Do> @naqvis Thank you :)
PixeLInc_ has joined #crystal-lang
maxpowa_ has joined #crystal-lang
andremed- has joined #crystal-lang
andremedeiros has quit [Ping timeout: 256 seconds]
PixeLInc has quit [Ping timeout: 256 seconds]
duane has quit [Ping timeout: 256 seconds]
baweaver has quit [Ping timeout: 256 seconds]
sagax has quit [Ping timeout: 256 seconds]
maxpowa has quit [Ping timeout: 256 seconds]
commavir has quit [Ping timeout: 256 seconds]
maxpowa_ is now known as maxpowa
andremed- is now known as andremedeiros
commavir_ has joined #crystal-lang
commavir_ is now known as commavir
teardown_ has joined #crystal-lang
teardown has quit [Remote host closed the connection]
aquijoule_ has quit [Remote host closed the connection]
aquijoule_ has joined #crystal-lang
_ht has quit [Remote host closed the connection]
ua has quit [Read error: Connection reset by peer]
ua has joined #crystal-lang
sz0 has quit [Quit: Connection closed for inactivity]
<FromGitter>
<kingsleyh> evening - is there anythign in Crystal that I can use to turn my crystal object into a canonical hash? I was using sha256(my_object.to_json) but it seems that the to_json might not always be coming out in the same order affecting the hashing. I would like to guarantee my hash always comes out the same
<FromGitter>
<oprypin:matrix.org> @kingsleyh: sort before calling `to_json`
<FromGitter>
<kingsleyh> obj.hash doesn't look like it will work
<FromGitter>
<kingsleyh> I guess I have to write something myself that takes the hash and processes it - it has many nested objects - so will have to process all of them
<FromGitter>
<Blacksmoke16> ah right, in your case you'd want the hash to be equal if the objs are equal
<FromGitter>
<Blacksmoke16> @kingsleyh the hash value will be the same within the same invocation of the program, but isn't guaranteed to be the same across multiple invocatiosn
<FromGitter>
<Blacksmoke16> as @oprypin:matrix.org pointed out
fifr` has quit [Ping timeout: 260 seconds]
<FromGitter>
<Blacksmoke16> whats the use case of needing this value?
<FromGitter>
<kingsleyh> my blockchain project - the block is hashed and set in the previous_hash field - so each block prev_hash must match the value of the previous block to hash
<FromGitter>
<kingsleyh> currently every now and again - the hash is different to the expected
<FromGitter>
<kingsleyh> which seems to be due to the block.to_json
<FromGitter>
<kingsleyh> I need a sha256 hash of the block that always produces the same hash for the same block
<FromGitter>
<kingsleyh> if the to_json decides to come out in a different order then the hash is different
<FromGitter>
<Blacksmoke16> as oprypin said, could you just ensure the data is sorted before calling `.to_json`
<FromGitter>
<Blacksmoke16> like an explicit sort
<FromGitter>
<kingsleyh> yeah - I can try - I have many custom data types and a lot of nesting - so it might be that I just need to write a new serializer that is canonical
<FromGitter>
<Blacksmoke16> 😬 sounds like a pita
<FromGitter>
<kingsleyh> I'm going to experiment with turning the object into a map and sorting it and turning it into a string
<FromGitter>
<oprypin:matrix.org> implementing JSON::Serializable with a custom to_json would also work
<FromGitter>
<oprypin:matrix.org> oh the two suggestions are basically the same suggestion :D
sorcus has quit [Ping timeout: 268 seconds]
<FromGitter>
<oprypin:matrix.org> i also would like to see the claim backed up - the claim that to_json can come out in a different order. i cant imagine how, unless a `Hash` is involved
<FromGitter>
<oprypin:matrix.org> @kingsleyh: haha yeah unfortunately Cannon has no guarantees. i think it requires same crystal version + same arch
<FromGitter>
<oprypin:matrix.org> @kingsleyh: but i shouldnt be so dismissive of icr. if to_json is coming out different, it doesnt matter that it's icr, it just shouldnt come out different anyway
<FromGitter>
<oprypin:matrix.org> would like to see it, anyway
<FromGitter>
<jrei:matrix.org> I don't understand?
<FromGitter>
<oprypin:matrix.org> you can't say in crystal CONT = 18
<FromGitter>
<oprypin:matrix.org> even though documentation says so
<FromGitter>
<jrei:matrix.org> If we follow your logic, `def write_bytes(object, format : IO::ByteFormat = IO::ByteFormat::SystemEndian)` should be changed?
sorcus has joined #crystal-lang
<FromGitter>
<jrei:matrix.org> that's just an alias
<FromGitter>
<oprypin:matrix.org> the value of the alias is platform dependent
<FromGitter>
<oprypin:matrix.org> currently all platforms happen to have the same value so on reality it currently happens to not be platform dependent
<FromGitter>
<jrei:matrix.org> I just provide the default use by IO::ByteFormat
<raz>
well, i guess in practice it would be fairly easy to notice and fix. but yea, i'd kinda expect a serialization lib to deserialize its data on all platforms by default :) (or a warning sticker in the readme)
<FromGitter>
<oprypin:matrix.org> do you or do you not have a feature that this lib can deserialize what this lib serialized?
<FromGitter>
<jrei:matrix.org> it can do both
* raz
shines desk lamp into jreis face
<raz>
hmm, he's still trying to weasel out!
avane has joined #crystal-lang
<FromGitter>
<oprypin:matrix.org> it can do both what
<FromGitter>
<jrei:matrix.org> serialize and deserialize
<FromGitter>
<oprypin:matrix.org> i thought ⏎ both deserialize and not deserialize