Stringified data doesn't get deserialized
See original GitHub issueFirst of all, I am a new to Polymer and trying to implement my project with lit-element. It seems like lit-element doesn’t deserialize stringified JSON data.
Here is what I wrote
static get properties() {
return {
name: String,
flavor: String,
data: Array
}
}
_render({ name, flavor, data }) { return this.template({ name, flavor, data }) }
It seems like when attributeChangedCallback gets triggered on data attribute with type of Array on PropertiesChanged, it executes _deserializeValue function which basically return same value since property type is array
_deserializeValue(value, type) {
switch (type) {
case Boolean:
return (value !== null);
case Number:
return Number(value);
default:
return value;
}
}
However what I expect is _deserializeValue function on PropertyAccessors
_deserializeValue(value, type) {
/**
* @type {*}
*/
let outValue;
switch (type) {
case Object:
try {
outValue = JSON.parse(/** @type {string} */(value));
} catch(x) {
// allow non-JSON literals like Strings and Numbers
outValue = value;
}
break;
case Array:
try {
outValue = JSON.parse(/** @type {string} */(value));
} catch(x) {
outValue = null;
console.warn(`Polymer::Attributes: couldn't decode Array as JSON: ${value}`);
}
break;
case Date:
outValue = isNaN(value) ? String(value) : Number(value);
outValue = new Date(outValue);
break;
default:
outValue = super._deserializeValue(value, type);
break;
}
return outValue;
}
Any suggestion on how to achieve this? Thanks in advance.
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (2 by maintainers)
Top Results From Across the Web
Stringified data doesn't get deserialized · Issue #77 - GitHub
First of all, I am a new to Polymer and trying to implement my project with lit-element. It seems like lit-element doesn't deserialize...
Read more >deserialize data from stringify data - Salesforce Stack Exchange
I have read many posts regarding this but I m not able to to deserialize the data . So please can anyone help...
Read more >Deserialize stringified JSON object - Stack Overflow
how do i get each of the 2 objects in "x" without specifying the name? Ideally, i'd like to convert this stringified JSON...
Read more >JSON Deserialization Techniques in Salesforce - OpFocus
Do you want to master JSON Deserialization techniques? Learn best practices from Salesforce experts on how to deserialize json c#.
Read more >JSON Class | Apex Reference Guide - Salesforce Developers
Contains methods for serializing Apex objects into JSON format and deserializing JSON content that was serialized using the serialize method in this class....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Based on the API that recently landed in master, you can customize but the serialization and deserialization behavior of a property by implementing the
type
. Here’s an example:@fillipio Oh I see it now. Well, there is a way, but it is kinda hacky, so I hope someone could get more useful idea.
I suggest to override function
_deserializeValue
for your component. I created a small example here. You can also create a basic component or a mixin that contains this overrided function.