0

I want to store a class into localstorage and therefor stringify it, and parse it back later, but scala.js (0.6.22) with the "builtin (?)" Closure compiler renames the class variables.

If I use the external Google closure compiler to optimize the not optimized fastopt javascript outputfile, variables keep there names, but the resulting file is larger.

Is there any way to take influence on the optimization-process besides scalaJSOptimizerOptions ~= { _.withDisableOptimizer(true/false) } ?

Joe
  • 85
  • 6
  • In vanilla JS, you cannot serialize a class instance into a string and expect to be able to deserialize it back either. It won't have the proper prototype. You can only do that for JSON objects. So you should use a serialization library to convert your instances to/from JSON and store the JSON in the `LocalStorage`. Again, in vanilla JS, you would need to do the same. – sjrd Mar 08 '18 at 14:15
  • It is not a JS problem but a scala.js fullOpt problem. With fastOpt everything is ok. Workaround: Using fastOpt and then calling Google closure compiler, results in a bigger but working JS file, – Joe Mar 17 '18 at 16:38
  • another workaround is to use a simple function instead of stringify, i.e. def toJSON() = { """{"id" : """" + this.id + """", "prename" : """" + this.prename + """", "lastname" : """" + this.lastname + """", "mobile" : """" + this.mobile + """"}""" } – Joe Mar 17 '18 at 16:59
  • Or, as I said, use a serialization library to do that for you, e.g., https://github.com/circe/circe. Both stringification (serialization) and parsing (deserialization). (and seriously, if it "works" in fastOpt, it is by chance; you cannot reliably `JSON.parse` a string back into an instance of a *class*; only to a plain JS object). – sjrd Mar 17 '18 at 19:06
  • yes, but I use the JS object to create a new case class object. I'll tryout circe. Thank you! – Joe Mar 18 '18 at 11:42

0 Answers0