Coder Social home page Coder Social logo

Returning some of a JSON array about argo HOT 4 CLOSED

sgoodwin avatar sgoodwin commented on May 23, 2024
Returning some of a JSON array

from argo.

Comments (4)

gfontenot avatar gfontenot commented on May 23, 2024

you could write your own parser to handle this. It would look a lot like the built in Array parser, but with catOptionals instead of sequence:

public func decodePartialArray<A where A: Decodable, A == A.DecodedType>(value: JSON) -> Decoded<[A]> {
  switch value {
  case let .Array(a): return catOptionals(A.decode <^> a)
  default: return .TypeMismatch("\(value) is not an Array")
  }
}

func catOptionals<T>(xs: [T?]) -> [T] {
    return xs.reduce([]) { accum, elem in
        return elem.map { accum + [$0] } ?? accum
    }
}

Then, in your decode function:

j <| "photos" >>- decodePartialArray

from argo.

tonyd256 avatar tonyd256 commented on May 23, 2024

Yes, it's doable, but you might want to look into why you have partial information about a photo. If it's just because you're dealing with bad data that you don't have control over then that's one thing but if you do control that then fixing the data source to return full or no data for an image might be easier and more reliable.

from argo.

gfontenot avatar gfontenot commented on May 23, 2024

You'll need to wrap that successful return statement in pure, too.

from argo.

sgoodwin avatar sgoodwin commented on May 23, 2024

Ah yes, that would work.

Tony: In this one case I have control over the API, but on most client projects I don't so I was curious.

Thanks again!

from argo.

Related Issues (20)

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.