jsonapi-serializer icon indicating copy to clipboard operation
jsonapi-serializer copied to clipboard

Deserialize/Serialize funcitons shouldn't update attribute names/keys of nested objects

Open pwalczyszyn opened this issue 9 years ago • 4 comments

In my model I have an attribute that has JSON object as it's value. This attribute is not part of my relationships and I want to pass it as JSON object as-is. When serializing/deserializing the attribute names/keys updates are done deeply also on nested JSON objects.

pwalczyszyn avatar Mar 11 '16 16:03 pwalczyszyn

I'm seeing similar behavior that causes problems later on in the app. This does not seem to be configurable either. I expect that the keys of people would remain as specified, and not be transformed to the default dash case.

const JSONAPISerializer = require('jsonapi-serializer').Serializer;
const data = [
	{id: 1, red_apples: 'yes', people: [{firstName: 'foo', lastName: 'bar'}]}
];
let config = {
	id: 'id',
	pluralizeType: false,
	attributes: ['red_apples', 'people'],
	people: {
		keyForAttribute: 'camelCase'
	}
};

console.log(new JSONAPISerializer('something', config).serialize(data).data[0].attributes.people);

ghost avatar Mar 15 '17 18:03 ghost

@SeyZ any thoughts on this? I can contribute a PR with tests to resolve it.

ghost avatar Jul 03 '17 16:07 ghost

We're running into this same issue as we have an attribute on a model that is meant to be JSON. We're using Ember Data and this attribute on the model is meant to be a "raw" attribute (DS.attr()). It would be nice if you could specify the values of certain attributes to not be processed and instead be passed through as is (in other words don't transform the keys).

I took a look at the PR provided (https://github.com/SeyZ/jsonapi-serializer/pull/147) and I think the option used ignoreNestedArray is a bit too specific (although thank you for providing the PR). What if your attribute is not an array but a JSON object and you don't want the keys of that object to be transformed?

I was thinking you could just have an option that is an array of attributes where the value for any listed attributes are not transformed. Maybe the option would be called rawAttributes.

 var json = new JSONAPISerializer('user', {
       rawAttributes: ['countries'],
       attributes: ['firstName', 'countries']
 }).serialize(dataSet);

This would still include countries in the final JSON API result (and it would transform the attribute key itself) but it would not modify the JSON object underneath it.

So for example if you had the following:

 var json = new JSONAPISerializer('user', {
       rawAttributes: ['myCountriesRaw'],
       attributes: ['firstName', 'myCountriesRaw', 'myCountries]
 }).serialize(dataSet);

you would end up with something like:

{
    "id": "1",
    "type": "collection",
    "attributes": {
          "my-countries-raw": {   // keys in here are not transformed
               "rawValue": [],
               "notTransformed": "blahblah"
           },
          "my-countries": {          // keys here are transformed
               "raw-value": [],
               "not-transformed": "blahblah"
           },
           "first-name": "Bob",
     }
}

@SeyZ Any thoughts on this? I would be happy to do a PR for it if you would accept it.

sarus avatar Dec 02 '17 16:12 sarus

@sarus good points. My initial PR is too specific since I wrote it simply to address our use case. I like your idea of a general parameter that can better solve the various needs. I think it makes sense to iterate on this idea to get a cleaner API we can all use. Then, hopefully, merge into the project.

ghost avatar Dec 04 '17 11:12 ghost