Object to text?

Hi,

I have this object inside Servoy:

	var x = {
		defaultView : 'agendaWeek',
		firstDay: 1, //0 = sunday 1= monday
		weekends: true,
		allDayText: 'gehele dag'
	}

But now I need this object as text (literally) to embed in a function, to fire it inside a webclient (by using the WebClientUtils plugin)
I tried: x.toString() but that does not work.

How can I convert this object, to a textstring, which has to be literally, everything between the { }
Hope someone can point me in the right direction. ;-)

json ?

I tried the function in Servoy fromJson, but the output contains all kinds of crap…

It has to be litteral

Hi Harjo,

I noticed that application.output(obj) does exactly that :)

However, you can use the following code to serialize the object to a string.

JSON.stringify(obj)

and

JSON.parse(str)

to de-serialize it back to an object.

So in your case this works:

	 var x = {
	      defaultView : 'agendaWeek',
	      firstDay: 1, //0 = sunday 1= monday
	      weekends: true,
	      allDayText: 'gehele dag'
	   }
	 var z = JSON.stringify(x);
	 /** @type {x} */
	 var q = JSON.parse(z)
	 application.output(q.allDayText);

Hope this helps.

JSON ??

I tried this in Servoy 6.0.7, but I got a warning, and error when I execute: ReferenceError: “JSON” is not defined.

Ai, sorry, probably need 6.1 for that then.

Try this instead:

	 var x = {
	      defaultView : 'agendaWeek',
	      firstDay: 1, //0 = sunday 1= monday
	      weekends: true,
	      allDayText: 'gehele dag'
	   }
	 var z = plugins.serialize.toJSON(x);
	 /** @type {x} */
	 var q = plugins.serialize.fromJSON(z);
	 application.output(q.allDayText);

that’s what I said, that produces crap!

{"_defaultView":"agendaWeek","javaClass":"org.mozilla.javascript.NativeObject","_allDayText":"gehele dag","_firstDay":1,"_weekends":true}

Works fine in 6.1 :mrgreen:

If all else fails then maybe this will help:

function serialize(obj)
{
  var returnVal;
  if(obj != undefined){
  switch(obj.constructor)
  {
   case Array:
    var vArr="[";
    for(var i=0;i<obj.length;i++)
    {
     if(i>0) vArr += ",";
     vArr += serialize(obj[i]);
    }
    vArr += "]"
    return vArr;
   case String:
    returnVal = obj;
    return returnVal;
   case Number:
    returnVal = isFinite(obj) ? obj.toString() : null;
    return returnVal;    
   case Date:
    returnVal = obj ;
    return returnVal;  
   default:
    if(typeof obj == "object"){
     var vobj=[];
     for(attr in obj)
     {
      if(typeof obj[attr] != "function")
      {
       vobj.push(attr + ':' + serialize(obj[attr]));
      }
     }
      if(vobj.length >0)
       return "{" + vobj.join(",") + "}";
      else
       return "{}";
    }  
    else
    {
     return obj.toString();
    }
  }
  }
  return null;

This is what it returns from your object:

{defaultView:agendaWeek,firstDay:1,weekends:true,allDayText:gehele dag}

You could use the VelocityReports plugin. It has a fromJson toJson function that doesn’t put a lot of java stuff in there.

JSON.stringify(obj) and JSON.parse(str) are new to 6.1 because of the updated Rhino engine. We moved to 6.1 just for this as much as anything else (and ended up liking a ton of other 6.1 features) because in our CMS we do this stuff all the time.

If you insist in 6.0 or earlier, you can use plugins.serialize.toJSON(obj) – the trick is to know the issues with types and then the pattern to the “crap” (I agree) it produces when applying JSON.parse(str) to it client-side.

1- Properties will have an underscore at the beginning when parsed client-side:

// server
var x = {
         defaultView : 'agendaWeek',
         firstDay: 1, //0 = sunday 1= monday
         weekends: "true",  // I changed value to a string
         allDayText: 'gehele dag'
      }
return plugins.serialize.toJSON(x)

// web client
var x = JSON.parse(str)
console.log(x._defaultView) // "agendaWeek"
console.log(x._firstDay) // "1"
console.log(x._weekends) // "true"
console.log(x._allDayText) "gehele dag"

2- Change boolean value to strings before serializing…ie, true becomes “true”.

3- Change number values to strings before serializing. Do this if you need to make sure to get the correct rounding. Integers ok to leave as integers.

4- Change dates to some string equivalent before serializing.

After de-serializing on the client-side, you will need to convert strings back into appropriate types if you’re going to further use them as javascript objects (calculations and such). Handling dates this way is especially tedious.

Pain in the ass eh? So move to 6.1 :)

ROCLASI:
You could use the VelocityReports plugin. It has a fromJson toJson function that doesn’t put a lot of java stuff in there.

I believe this function only shows up when using 6.1.

Thanks guys,

David convinced me to try this in 6.1 ;-)

So with that JSON function, you don’t have to convert dates, integers, strings, etc… to all strings?

david:

ROCLASI:
You could use the VelocityReports plugin. It has a fromJson toJson function that doesn’t put a lot of java stuff in there.

I believe this function only shows up when using 6.1.

This is Patrick’s plugin.
I know for a fact that this works in 5.2 and 6.0 as well. Just make sure you use the latest version.

Yes, Robert is right: the latest v2.0.x version of VelocityReports expose fromJSON and toJSON methods and this is working fine with any Servoy from 5.2 to 6.1 - haven’t tested lately but I suppose it would also work im 4.1.x…

ptalbot:
Yes, Robert is right: the latest v2.0.x version of VelocityReports expose fromJSON and toJSON methods and this is working fine with any Servoy from 5.2 to 6.1 - haven’t tested lately but I suppose it would also work im 4.1.x…

Cool. Would have helped a whole bunch of lot last year :)