question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stringified data doesn't get deserialized

See original GitHub issue

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 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:closed
  • Created 5 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sorvellcommented, Aug 27, 2018

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:

export const ObjectType = {
  {
    fromAttribute: (value) => {
      let result;
      try {
        result = JSON.parse(value);
      } catch(x) {
        result = value;
        console.warn(`Could not JSON.parse value ${value}`);
      }
      return result;
    }
    toAttribute: (value) => {
      try {
        return JSON.stringify(value);
      } catch(x) {
        console.warn(`Could not JSON.stringify value ${value}`);
        return '';
      }
    }
}

// then in some element...
static get properties() {
  foo: {type: ObjectType }
} 
1reaction
Lodincommented, May 25, 2018

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found