Comments (7)
Hi Michal, any chance you're interested in something like this landing in the library? Our use case is that most APIs out there expect camelCase keys. One approach is of course to transform the keys in the map before encoding, but this is more inefficient I guess.
from jason.
One possible way to handle this is similar to the solution I propsoed in #119:
Build a wrapper for maps, where this special handling is required, that hijacks the JSON generation though the protocol:
defmodule Wrapper do
defstruct [:value]
def new(map) when is_map(map), do: %__MODULE__{value: map}
defimpl Jason.Encoder do
import Jason.Encode, only: [value: 2, string: 2]
def encode(%{value: map}, opts) do
case Map.to_list(map) do
[] ->
"{}"
[{key, value} | rest] ->
["{", key(key, opts), ":", value(value, opts), encode_loop(rest, opts), "}"]
end
end
defp encode_loop([{key, value} | rest], opts) do
[",", key(key, opts), ":", value(value, opts) | encode_loop(rest, opts)]
end
defp key(other, opts), do: string(camelize(to_string(other)), opts)
defp camelize(string), do: .... # do actual conversion to camelCase
end
end
This does a lot of the handling manually, but does avoid double traversal of the data structures and should have comparable performance to native encoders in Jason itself. At this point, it's possible to wrap the maps where you need this special handling with Wrapper.new(map)
and pass that to Jason.encode!/2
.
Because of the concerns stated above, I don't think we'll have a built-in option for key conversion on encoding in Jason itself. I'm therefore going to close the issue.
from jason.
Do you mean when encoding? Right now there's generally no transformation performed on keys when encoding besides converting to strings according to the String.Chars
protocol.
If you're asking about decoding, there's already an option where you can provide a function for transforming keys - the primary use-case is to change keys to atoms, but it could also be used to change casing.
from jason.
Hi! Yes, I meant when encoding, sorry for not making it clear.
In a few projects, where I use Jason, there is value in camelCase JSON and the frontend expects it accordingly.
Thank you for your response and your time.
from jason.
Something you could do until this is supported:
This doesn't necessarily solve the problem (there is PR up it seems) but one way to handle this from a JS perspective:
// you can cast vars while destructuring
const {
foo_bar: fooBar,
bar_baz: barBaz,
} = data;
const someApiData = {
fooBar,
barBaz,
};
// pretend you have a redux store or something
this.props.dispatch({
type: 'STORE_SOME_API_DATA',
someApiData,
});
However if jason does convert to camel case, it could be just as easy as:
const someApiData = { ...data };
Just food for thought π
from jason.
@michalmuskala: we would be interested in reviving PR #56 if this is something you would consider merging.
Does you like that approach? It seems like it would match well with the existing option in decode
mentioned above...
from jason.
The big issue is that options like this wouldn't be supported in derived struct implementations (since they pre-encode keys at compile-time). Not to mention things like Jason.Fragment
that are completely pre-encoded. So it's problematic to support those things in a uniform way that wouldn't be surprising in some circumstances.
from jason.
Related Issues (20)
- Problem decoding a array with value "[88]", returns "X" HOT 1
- feature request: Filtering function for maps HOT 1
- Jason.decode crashes when encountering complex emoji codepoints HOT 4
- Decode failing when string has lots of spaces and special characters HOT 1
- Jason opts argument .encode clarification HOT 4
- Jason.Decoder is not a protocol HOT 1
- html_safe encoding bug HOT 1
- Jason.decode timeouts silently with large files HOT 5
- Jason fails to encode HOT 1
- Better error messaging on invalid escapes
- Issue decoding particular string HOT 5
- fails to install in rebar3 HOT 2
- Proposal: Consistent number decoding
- Configure Jason to parse keys as atoms by default for Phoenix HOT 2
- Support JSON Canonical Scheme (JCS) - RFC 8785 HOT 2
- Json file can not be decoded HOT 1
- Heads up: Jason will be an ExDoc dependency HOT 1
- Map key validity/order in Elixir 1.14 HOT 3
- Misformatted README HOT 1
- Lists with numbers can parse to ASCII whitespace HOT 2
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 jason.