<FromGitter>
<mixflame> hope this removes the roadblock for future developers
<postmodern>
i've seen people post things that were just basically TIL posts. Well they may have edited it afterwards.
chillfox has joined #crystal-lang
<FromGitter>
<mixflame> ahh good idea
<jhass>
oprypin: heh, whatever works I guess
<FromGitter>
<naqvis> Hi jhass, nice to see you around :P
<FromGitter>
<naqvis> haven't seen you around since quite some time, hope things are well
<jhass>
sure, was just spending time more outside while it was still warm over here :)
<FromGitter>
<naqvis> nice
<FromGitter>
<guglielmocg_gitlab> Pardon me for the noob question, but when I want to create an array of JSON with JSON::ArrayBuilder, what is the T parameter? I can't help finding the doc a bit cryptic about this
<FromGitter>
<guglielmocg_gitlab> @mixflame that blog idea would be a great thing
<jhass>
there's no JSON::ArrayBuilder?
<jhass>
might be easier to show what kind of data you have and what the generated JSON should look like
<jhass>
JSON is not a datatype in Crystal, so you cannot have an "array of JSON"
r0bby has quit [Ping timeout: 272 seconds]
r0bby has joined #crystal-lang
<FromGitter>
<j8r> but you can have an array of JSON::Any
<jhass>
sure but that's what they got would be a pure assumption, better to ask :)
<FromGitter>
<guglielmocg_gitlab> The response to HTTP::Client.get("#{base_url}", headers: HTTP::Headers{"Accept" => "application/json", "api-key" => "#{ENV["SIB_API"]}"}) is ⏎ { ⏎ ⏎ ```code paste, see link``` ⏎ ... [https://gitter.im/crystal-lang/crystal?at=5f46291b4823780937437e86]
<FromGitter>
<guglielmocg_gitlab> But I need, ideally, to have a Hash, or to serialize it into a JSON (or JSON::Any). I believe this would need two nested serializers since the structure is ⏎ "items" { "events"[ "email": "", "date": "", "subject": "" ... ] }
<jhass>
you seem a little confused about terminology. JSON is a serialzation format. The string representation is the serialized form. Going from string to datastructure is called deserialization
<jhass>
I take it you have well structured data from some API endpoint. I'm not sure what you endgoal with that data is, because you described none of your goals, but I would guess the cleanest code is if you were to build a couple of JSON::Serializable based structs
<FromGitter>
<guglielmocg_gitlab> This is entirely possible since I'm new to the industry and mostly worked in Ruby. I'm used to a much less complex type system.
<jhass>
the terminology remark is entirely independent of any language or type system, it's a rather abstract concept
<FromGitter>
<guglielmocg_gitlab> I admit I have much to learn, but I can't get around the fact that ⏎ pp! Hash(String, Lucky::Response).new(HTTP::Client.get("#{base_url}", headers: HTTP::Headers{"Accept" => "application/json", "api-key" => "#{ENV["SIB_API"]}"})) ⏎ makes the compiler go: ⏎ In lib/lucky/src/lucky/renderable.cr:124:5 ⏎ web | ... [https://gitter.im/crystal-lang/crystal?at=5f462b9edfaaed4ef50e2d0c]
<FromGitter>
<guglielmocg_gitlab> It's what I'm gonna do but it's gonna be quite bloated, unless it is possible to nest JSON::Serializable structs. Is it?
<jhass>
again, I have no clue as to what your endgoal is
<FromGitter>
<guglielmocg_gitlab> sorry for not being clear. My goal is to take the result of the request and turn it ideally into a hash
<FromGitter>
<guglielmocg_gitlab> or have it as a hash
<FromGitter>
<guglielmocg_gitlab> A JSON::Any would also do
<FromGitter>
<j8r> `JSON::Any` should only be used if you don't know the mapping at all
<FromGitter>
<j8r> otherwise using proper types will be better
<FromGitter>
<guglielmocg_gitlab> @j8r so something like JSON(String, String).new?
<FromGitter>
<j8r> I mean, to what extend you know the mapping? It is just a Hash of "random" keys, values?
<jhass>
"turn it into a hash" is not your goal, it's the solution towards your goal that you envision. Classic XY question :)
<FromGitter>
<j8r> What jhass showed can also be applied for a request
<FromGitter>
<guglielmocg_gitlab> Oh I get the misunderstanding! I need to return ordered, useable data from this request to my esteemed frontend colleague
<jhass>
so you showed an example of the source data, how would the response based on that look like?
<FromGitter>
<guglielmocg_gitlab> Oh shoot @jhass I had missed the carc.in link you sent me. That looks really good
<FromGitter>
<guglielmocg_gitlab> That shows me how to nest serializables. This is so good. I tried it but my syntax was not good.
<jhass>
so if it's just about sorting the events array, you can do that and use .to_json anywhere to serialize it back into JSON (it optionally takes an IO argument for streaming response)
<jhass>
if you're remapping, I'd make a second set of structs and give them constructors that take the first set of structs
<FromGitter>
<guglielmocg_gitlab> I'm still getting my head around IO arguments for JSON
<FromGitter>
<mwlang> No worries, @wyhaines, I got it sorted. The whole intent was to understand crystal better and esp. when what I write in Ruby doesn't quite translate to crystal and this was a good exercise to that end.
<FromGitter>
<mwlang> I like that last version you came up with. no idea why I didn't think to tack `#map` on to the outside of the block.
[gnubie] has joined #crystal-lang
<FromGitter>
<j8r> @guglielmocg_gitlab in fact, you can pass either a String or any IO
alexherbo2 has joined #crystal-lang
[gnubie] has quit [Ping timeout: 256 seconds]
duane has joined #crystal-lang
duane has quit [Read error: Connection reset by peer]
duane has joined #crystal-lang
alexherbo2 has quit [Ping timeout: 260 seconds]
alexherbo2 has joined #crystal-lang
alexherbo23 has joined #crystal-lang
alexherbo2 has quit [Ping timeout: 260 seconds]
alexherbo23 is now known as alexherbo2
andremedeiros has quit [Quit: ZNC 1.8.1 - https://znc.in]
<Andriamanitra>
and i can even make the chain as long as i want and it still just seems to work
alexherbo2 has quit [Ping timeout: 258 seconds]
<FromGitter>
<Blacksmoke16> prob because in the other 3 examples you're comparing a `Bool` with a `Int32`
<FromGitter>
<Blacksmoke16> which aren't comparable
<Andriamanitra>
yes, i get why those don't work, what my question is why the first one DOES work
<Andriamanitra>
in ruby "1 < 2 < 3" would result in "NoMethodError: undefined method `<' for true:TrueClass"
<FromGitter>
<Blacksmoke16> :shrug: compiler is just smart enough to see they're all numbers
<FromGitter>
<Blacksmoke16> is what i would say
alexherbo2 has joined #crystal-lang
<FromGitter>
<Blacksmoke16> either that or it just gets optimized out
<FromGitter>
<dscottboggs> no this is a feature that is not in ruby that is in crystal
<Andriamanitra>
there must be some kind of special case where the compiler checks if there's more comparison operators after the first one but how does that even work if i have a custom #< method
<FromGitter>
<dscottboggs> putting the operators next to each other follows python-like semantics in Crystal
<FromGitter>
<dscottboggs> the only exception being that obviously Crystal has no `in` operator like Python does (instead we use the `#includes?` method)
<FromGitter>
<Blacksmoke16> there is `in?`
<FromGitter>
<dscottboggs> oh I didn't know that, nice
<FromGitter>
<dscottboggs> wait, is that an operator or method?
<FromGitter>
<Blacksmoke16> also usually you can do `===` for that
<FromGitter>
<dscottboggs> really?? Nice!
<FromGitter>
<Blacksmoke16> like `1 === 0..10 # => true`
alexherbo2 has quit [Ping timeout: 264 seconds]
<FromGitter>
<Blacksmoke16> er maybe not
<FromGitter>
<Blacksmoke16> `(0..10) === 1` this way
<FromGitter>
<Blacksmoke16> as its an overload of `Range` for example
<FromGitter>
<dscottboggs> my point is that python has a `in` operator which has some weird semantics around operator *chaining* like in the example. I think I saw like `False == True in [False]` or something like that has a weird result in python. that wouldn't happen in Crystal because the equivalent would be `false == true.in? [false]` or `(false == true).in? [false]` which is more obvious
<FromGitter>
<Blacksmoke16> same magic it does for the first example
alexherbo2 has quit [Ping timeout: 240 seconds]
alexherbo2 has joined #crystal-lang
alexherbo2 has quit [Ping timeout: 258 seconds]
duane has joined #crystal-lang
alexherbo2 has joined #crystal-lang
<Andriamanitra>
i guess the moral of the story is if you define comparison methods in a silly way, expect them to behave in silly ways too https://play.crystal-lang.org/#/r/9lfk