Comments (5)
FYI this is available via onv1.5.0
https://github.com/joanllenas/ts.data.json/releases/tag/v1.5.0
from ts.data.json.
Hi @halogenandtoast , that's an interesting feature to have.
I'll give it a thought.
Thanks.
from ts.data.json.
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.
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.
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)
- Add better string validation HOT 6
- Public functions in the class are identifie as a property. HOT 1
- ts.data.json 2.0? HOT 3
- How to decode discriminated union types with nested json? HOT 4
- Add documentation about Decoder operations HOT 1
- FR: Easier to navigate (and complete) API reference HOT 3
- Improve the case where the input to a decoder is not an object HOT 3
- FR: Decode to class instance HOT 1
- How to use JsonDecoder.dictionary for decoding a Map instance? HOT 4
- Question? HOT 2
- Just wanted to say thanks HOT 1
- Typescript helper function to extract decoder type HOT 3
- How would you decode an empty object HOT 3
- Wish List HOT 8
- Not working with Artifactory because of unfortunate name HOT 2
- Optionally fail object decoder when encountering unknown keys HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ts.data.json.