Code Monkey home page Code Monkey logo

Comments (5)

joanllenas avatar joanllenas commented on August 26, 2024 1

FYI this is available via onv1.5.0
https://github.com/joanllenas/ts.data.json/releases/tag/v1.5.0

from ts.data.json.

joanllenas avatar joanllenas commented on August 26, 2024

Hi @halogenandtoast , that's an interesting feature to have.
I'll give it a thought.
Thanks.

from ts.data.json.

svdHero avatar svdHero commented on August 26, 2024

I was about to post the same issue. I have any array of tuples and need to write a decoder for that.
@halogenandtoast do you have an easy work-around for this problem and would you be willing to share a code snippet?

from ts.data.json.

svdHero avatar svdHero commented on August 26, 2024

For my special case, I came up with this:

type StringPair = readonly [string,string]
type ConfigInfo = readonly StringPair[]

function isArray(value: any): value is any[] {
    return Array.isArray(value)
}

function jsonDecoderErrorMessagePrimitive(value: any, tag: string): string {
    return `${JSON.stringify(value)} is not a valid ${tag}`
}

const stringPairDecoder: JsonDecoder.Decoder<StringPair> = new JsonDecoder.Decoder(value => {
    if(isArray(value)){
        if(value.length===2) {
            const fst = JsonDecoder.string.decode(value[0])
            if(fst.isOk()){
                const snd = JsonDecoder.string.decode(value[1])
                if(snd.isOk()){
                    return ok<StringPair>([fst.value,snd.value])
                }
            }
        }
    }
    return err<StringPair>(jsonDecoderErrorMessagePrimitive(value, "StringPair"))
})

const configInfoDecoder: JsonDecoder.Decoder<ConfigInfo> =
    JsonDecoder.array(stringPairDecoder, "ConfigInfo")

I haven't bothered with a more generic and reusable workaround, yet. Any ideas?

from ts.data.json.

halogenandtoast avatar halogenandtoast commented on August 26, 2024

My "solution" was to forgo doing any checks on the structure and just using JsonDecoder.succeed but at some point I may try writing a real decoder for the numerous tuples I have.

from ts.data.json.

Related Issues (17)

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.