3
Lensflare
20h

Currently implementing a Swift devRant SDK and encountered something very stupid:

The json value for attached image is an object (dictionary) when there is an image but it‘s an empty string "" when there is no image.

So two different types for the same property.

I hate this kind of crap.

Why not make it null or omit that property when there is no image?

Now I need to add ugly as fuck custom decoding code for this object.

Comments
  • 0
    isnt there a "NullValueHandling = NullValueHandling.Ignore" equivalent when deserialising?
  • 0
    @azuredivay the issue is that it‘s not null but an empty string
  • 0
    what's the problem? You don't have union operators?
  • 0
    @antigermanist I know union types (not operators) from typescript.
    Declaring multiple possible types doesn‘t help the issue because the decoder still wouldn‘t know what type to use.
    All it could do is to try and decode those types one after another until one succeeds.
    And that‘s exactly what I am doing with custom decoding.

    It‘s not an unsolvable problem. It‘s just shit api design and ugly decoding code.
  • 0
    @Lensflare say what u want, typescript is pretty nice with this. You could just do something like

    type Image = ImageProps | ""'
  • 0
    @Lensflare but yes if the decoder want static typing that wouldn't work. Weird though, because nothing in the json specs says anything about props having specific types.
  • 0
    @Lensflare anyway there's always edge cases. maybe abstract it away at least so it doesn't pollute the main code
  • 0
    @antigermanist of course it will be abstracted away. That’s the whole point of the sdk: it provides nice and clean code to access devrant data with nice and clean model types with proper names and structure.

    All the dirty mapping and decoding code is hidden.

    Though the declarative mapping code can also be seen as doc for the devrant api.
    And that‘s where the ugly custom mapping code gets in the way.
  • 1
    @Lensflare well i'll prolly never use swift but

    thanks i guess <3
  • 0
    @antigermanist that‘s ok. I mainly write it for myself.

    It would be nice if someone else would actually use it but thats unlikely.
Add Comment