Reputation: 10549
I am about to start development on an Elm application that is mostly about displaying a data set. The data will be prepared in several JSON files (including different languages for text), specified by JSON schemas. This data base will not go away, since other use cases for the shared dataset exist.
I see two options for accessing these data from Elm now.
Using json-schema-to-elm or similar, I can generate data types and parsers from the schemas I have. Then, at runtime, I load the JSON the app needs and parse it.
Advantages
Disadvantages
With a hand-written compiler (maybe based on the types generated by json-schema-to-elm), I can statically convert the JSON data into Elm code. The data thus ship with the app and can be accessed using Elm primitives.
Advantages
Disadvantages
Based on above listing, here is my conclusion.
Therefore, I think that going with precompiled Elm values is the better solution in my case.
My question is: Have I missed any aspects of either approach that would impact my tradeoff? Are there other approaches I should consider?
Note that I'm not worried about the specific tools right now; this is more of a conceptual, design question.
Upvotes: 2
Views: 431
Reputation: 10549
Here is an idea.
json-schema-to-elm
.toString
and write the result to a file.According to the documentation, the result of toString
should look just like the value it came from.
The efficacy of this approach depends on:
json-schema-to-elm
is an Elixir application, and Elm is not really scriptable.json-schema-to-elm
support your schema.In my case, json-schema-to-elm
does not accept the schema (with less than helpful error messages), and it does not seem to support patternProperties
at all (and possibly other features of JSON schema).
Upvotes: 0
Reputation: 36375
To consolidate the conversation in the above comments:
The two packages you mentioned, json-schema-to-elm
and json-to-elm
are roughly similar in their output. They both render Elm source code containing types, decoders, and encoders.
The main difference is in their input:
json-schema-to-elm
takes JSON Schema as input. This is useful when your JSON becomes more complex than can be described with an example, but it also requires that you write a schema file for all the JSON you want modeled.json-to-elm
takes example JSON values as input. This is useful when your JSON model is relatively simple.Personally, I'd try to write some proof of concepts to see if there really are any detrimental runtime inefficiencies. You could always just keep json values as strings in the .elm file - that would ease offline access, avoid network traffic, and really the only downside is a once-per-value decoding of each json input, since if it doesn't change, you won't need to decode it again.
Note: if you go the route of embedding json as string values in .elm files, be aware of the multi-line string syntax which will help avoid lots of escape characters in raw json strings
Upvotes: 2