Code Monkey home page Code Monkey logo

nullcodable's Introduction

@NullCodable

@NullCodable is a property wrapper that encodes nil optional values as null when encoded using JSONEncoder.

On its own, JSONEncoder will omit optional properties that are nil - meaning that this:

struct Test: Codable {
  var name: String? = nil
}

will be encoded as: {}.

If for some reason, you would like optional properties that are nil to be encoded in JSON as null, then marking those properties as @NullCodable will do so.

For example, adding @NullCodable like this:

  struct Test: Codable {
    @NullCodable var name: String? = nil
  }

will encode as: {\"name\": null}.

nullcodable's People

Contributors

austinpayne avatar g-mark avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nullcodable's Issues

Thanks for NullCodable, and some notes

First of all, thanks for the snippet! It saved me a lot of time and eventually helped understand how the Encoder/Decoder interfaces work a bit better.

I'm writing an encoder/decoder pair for MySQL-NIO as a part of a new server framework - will open the repo once it's ready. A few things I noticed:

  • NullCodable is good for any type of encoders/decoders, not just JSON ones, so you can mention this in your README. For example, there's Apple's PropertyListDecoder, and any custom ones coming from other developers.
  • The extension KeyedDecodingContainer part is probably not needed. At least in my case everything works with or without that part. It might be, however, that this extension shortens the decoding path, i.e. NullCodable(from:) won't be called in certain situations (but not others). But I'm not entirely sure about this.

Custom dateEncodingStrategy

Great library, using @NullCodable on a Date it converts it to seconds. Would it be possible to add iso8601 option?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.