0

This is for a gaming application. In my game I want to save special effects on a player in a single field of my database. I know I could just put a refrence Id and do another table and I haven't taken that option off the table. Edit: (added information) This is for my server in node not the browser.

The way I was thinking about storing the data is as a javascript object as follows:

effects={
           shieldSpell:0,
           breatheWater:0,
           featherFall:0,
           nightVision:0,
           poisonResistance:0,
           stunResistance:0,
           deathResistance:0,
           fearResistance:0,
           blindResistance:0,
           lightningResistance:0,
           fireResistance:0,
           iceResistance:0,
           windResistance:0}

It seems easy to store it as a string and use it using effects=eval(effectsString) Is there an easy way to make it a string or do I have to do it like:

effectsString=..."nightVision:"+effects.nightVision.toString+",poisonResistance:"+...
3
  • what's your server side language? have you considered saving user datas as XML files and just adding the filename to you user's table? XML is the right way to store structured datas, you can even style it and make it render preatty on brawser visualization. When you have your xml saved on the server you just need to open it and call something like json_econde before returning it to the client and there you go! Commented Aug 7, 2012 at 14:52
  • The server is run in node.js and communicates to a db via mySQL Commented Aug 7, 2012 at 14:53
  • then you should try and use a CouchDB which is a non mySQL DB and can store JSON objects so you'll never have to convert it to a string - couchdb.apache.org Commented Aug 7, 2012 at 14:57

4 Answers 4

7

Serialize like that:

JSON.stringify(effects);

Deserialize like that:

JSON.parse(effects);
Sign up to request clarification or add additional context in comments.

6 Comments

I got this effor when I tried hat... SyntaxError: Unexpected token o
Do you get this error on stringify or parse? Be more specific please
I get the error only when I parse, I used the above data in java-script console.
The above object effects is no JSON object. Therefore you get an error. JSON.parse(JSON.stringify(effects)) should work
Great, you might also want to accept that answer if it suits you right.
|
2

Use JSON.stringify

That converts a JS object into JSON. You can then easily deserialize it with JSON.parse. Do not use the eval method since that is inviting cross-site scripting

//Make a JSON string out of a JS object
var serializedEffects = JSON.stringify(effects);
//Make a JS object from a JSON string
var deserialized = JSON.parse(serializedEffects);

6 Comments

I'm doing this in node on my server. Do I need to worry about XSS attacks if its not on the browser?
@Shawn You don't have to worry about it on the server, if the input is never coming from the client. However, there's no reason not to use JSON.parse, since that's supposedly faster because it doesn't have to start up a new JS execution context.
Just trying to work out the error when I parse then...SyntaxError: Unexpected token o
@Shawn, did you serialize it using JSON.stringify? If you did, it should be impossible to get a SyntaxError. Does it work if you use eval? That's usually because properties aren't quoted with double quotes (if you serialized it yourself)
No I did this and got the error. Copy to js console if you want. effects={ shieldSpell:0, breatheWater:0, featherFall:0, nightVision:0, poisonResistance:0, stunResistance:0, deathResistance:0, fearResistance:0, blindResistance:0, lightningResistance:0, fireResistance:0, iceResistance:0, windResistance:0}; JSON.stringify(effects); parsedString =JSON.parse(effects); SyntaxError: Unexpected token o
|
0

JSON parse and stringify is what I use for this type of storatge

var storeValue = JSON.stringify(effects); //stringify your value for storage
// "{"shieldSpell":0,"breatheWater":0,"featherFall":0,"nightVision":0,"poisonResistance":0,"stunResistance":0,"deathResistance":0,"fearResistance":0,"blindResistance":0,"lightningResistance":0,"fireResistance":0,"iceResistance":0,"windResistance":0}"
var effects = JSON.parse(storeValue);  //parse back from your string

Comments

0

Here was what I've come up with so far just wonering what the downside of this solution is.

        effectsString="effects={"
        for (i in effects){
           effectsString=effectsString+i+":"+effects[i]+","
        }
        effectsString=effectsString.slice(0,effectsString.length-1);
        effectsString=effectsString+"}"

Then to make the object just

eval(effectsString)

1 Comment

The downside is that you're reinventing the wheel. Also, using slice to remove that last comma is so inelegant. Moreover, this will fail if any of your properties contain values that need to be escaped (like quotes). Also, setting variables using eval is also inelegant, even if you had to use this method, you would do effects = eval("jsonstring") instead of setting a variable within the eval. Not sure why you're not using what we suggested, stick with JSON.parse and JSON.stringify.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.