Object to text?

Using Servoy to administrate the content of your website? Discuss all webrelated Servoy topics on this forum!

Object to text?

Postby Harjo » Wed Jul 18, 2012 9:11 am

Hi,

I have this object inside Servoy:

Code: Select all
   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. ;-)
Harjo Kompagnie
ServoyCamp
Servoy Certified Developer
Servoy Valued Professional
SAN Developer
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Re: Object to text?

Postby Hans Nieuwenhuis » Wed Jul 18, 2012 9:26 am

json ?
Hans Nieuwenhuis
Betagraphics
http://www.deltics.nl
http://www.betagraphics.nl

Servoy Version 7.3.1
Java version 1.7.0.x
Database Oracle 11g
User avatar
Hans Nieuwenhuis
 
Posts: 1026
Joined: Thu Apr 12, 2007 12:36 pm
Location: Hengelo, The Netherlands

Re: Object to text?

Postby Harjo » Wed Jul 18, 2012 9:47 am

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

It has to be litteral
Harjo Kompagnie
ServoyCamp
Servoy Certified Developer
Servoy Valued Professional
SAN Developer
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Re: Object to text?

Postby omar » Wed Jul 18, 2012 10:11 am

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.

Code: Select all
JSON.stringify(obj)


and

Code: Select all
JSON.parse(str)


to de-serialize it back to an object.

So in your case this works:

Code: Select all
    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.
Intrasoft, Founder
Omar van Galen
omar@intrasoft.nl
+31-(0)6-21234586
Servoy Developer
omar
 
Posts: 377
Joined: Sat Feb 12, 2011 4:51 pm
Location: Intrasoft, The Netherlands

Re: Object to text?

Postby Harjo » Wed Jul 18, 2012 10:16 am

JSON ??

I tried this in Servoy 6.0.7, but I got a warning, and error when I execute: ReferenceError: "JSON" is not defined.
Harjo Kompagnie
ServoyCamp
Servoy Certified Developer
Servoy Valued Professional
SAN Developer
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Re: Object to text?

Postby omar » Wed Jul 18, 2012 10:23 am

Ai, sorry, probably need 6.1 for that then.
Intrasoft, Founder
Omar van Galen
omar@intrasoft.nl
+31-(0)6-21234586
Servoy Developer
omar
 
Posts: 377
Joined: Sat Feb 12, 2011 4:51 pm
Location: Intrasoft, The Netherlands

Re: Object to text?

Postby omar » Wed Jul 18, 2012 10:28 am

Try this instead:

Code: Select all
    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);
Intrasoft, Founder
Omar van Galen
omar@intrasoft.nl
+31-(0)6-21234586
Servoy Developer
omar
 
Posts: 377
Joined: Sat Feb 12, 2011 4:51 pm
Location: Intrasoft, The Netherlands

Re: Object to text?

Postby Harjo » Wed Jul 18, 2012 10:40 am

that's what I said, that produces crap!

Code: Select all
{"_defaultView":"agendaWeek","javaClass":"org.mozilla.javascript.NativeObject","_allDayText":"gehele dag","_firstDay":1,"_weekends":true}
Harjo Kompagnie
ServoyCamp
Servoy Certified Developer
Servoy Valued Professional
SAN Developer
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Re: Object to text?

Postby omar » Wed Jul 18, 2012 11:01 am

Works fine in 6.1 :mrgreen:

If all else fails then maybe this will help:

Code: Select all
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:

Code: Select all
{defaultView:agendaWeek,firstDay:1,weekends:true,allDayText:gehele dag}
Intrasoft, Founder
Omar van Galen
omar@intrasoft.nl
+31-(0)6-21234586
Servoy Developer
omar
 
Posts: 377
Joined: Sat Feb 12, 2011 4:51 pm
Location: Intrasoft, The Netherlands

Re: Object to text?

Postby ROCLASI » Wed Jul 18, 2012 6:38 pm

You could use the VelocityReports plugin. It has a fromJson toJson function that doesn't put a lot of java stuff in there.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: Object to text?

Postby david » Wed Jul 18, 2012 6:48 pm

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:

Code: Select all
// 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 :)
David Workman, Kabootit

Image
Everything you need to build great apps with Servoy
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Re: Object to text?

Postby david » Wed Jul 18, 2012 6:49 pm

ROCLASI wrote: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.
David Workman, Kabootit

Image
Everything you need to build great apps with Servoy
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Re: Object to text?

Postby Harjo » Wed Jul 18, 2012 8:14 pm

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?
Harjo Kompagnie
ServoyCamp
Servoy Certified Developer
Servoy Valued Professional
SAN Developer
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Re: Object to text?

Postby ROCLASI » Wed Jul 18, 2012 8:14 pm

david wrote:
ROCLASI wrote: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.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: Object to text?

Postby ptalbot » Thu Jul 19, 2012 3:03 am

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...
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Next

Return to Web Development

Who is online

Users browsing this forum: No registered users and 6 guests

cron