Json cursor parsing

Developer
Oct 15, 2013 at 6:18 PM
Why can't I parse the serialized cursors ?
e.g.
1)
SearchResults is a cursor. Serialize it.
aa = oJSON.stringify("SearchResults")

2)
Now try making cursor out of this 'aa' json object. I expect a table named (bb) to be created.
bb = oJSON.parse(aa)

3)
No table gets created and ?USED(bb) command returns an error message.
Coordinator
Oct 17, 2013 at 2:12 AM
Titu,

Is this for version 603? I experienced a bug in version 553 which forced me to add another prg called JSON2.PRG. There was a thread on this issue:
https://activevfp.codeplex.com/discussions/449603

Which summarize the things that I discovered:
I have found the obvious since I was using version aVFP 5.XX. The jSON implementation included in this version that I have seems buggy. When I added the latest version from Craig's into my project, it worked like a charm. I will try to post some sample code on this thread once I completed my work.

I cannot comment of 603 since I'm just beginning to use it and founding things that makes me pull my hair again. :)
Developer
Oct 17, 2013 at 4:29 AM
yikes..
thanks for the heads-up apaustria. I'll re-try after loading Craig's latest release.
Developer
Oct 17, 2013 at 10:36 PM
Edited Oct 17, 2013 at 10:48 PM
Hi apaustria,

Did you manage to get ajax POST working in AVFP? I am sending serialized Json to server, but I cannot find it in oRequest.forms. Any idea's from where I can flush it out?
BTW, I trying to use RESTful module for processing this data.

Thx.

If you have any link to Jquery mobile on this topic.. it would help me a lot.
Coordinator
Oct 17, 2013 at 11:29 PM
Yes. My code for sending the serialized form look like this.

function savesomething(){
var formjson = JSON.stringify($('#examforms').serializeObject());
var urlprocess = "<%=oProp.ScriptPath+[?action=saveexam]%>";

$.ajax({
  url: urlprocess,
  type: 'POST',
  data: { examformjson: formjson },
  beforeSend: function() {
    notify('Saving exams...', 'Please wait...', {
        icon: 'img/demo/icon.png'
        });                
  },
  complete: function() {
      // hide indicator
      setTimeout(function() {
        document.location.href = '<%=oProp.ScriptPath +[?action=showsavedexam]%>' ;
        }, 2000);
     },
  success: function(data) {
      //alert('xx');
  }
});
}


In my PRG it looks like this.

CASE oProp.Action=="saveexam"
oJSON=NEWOBJECT('json2')        && need to use JSON2 since JSON (orig) is broken on this aVFP version.
oJSON.ParseRespectClass = .T.
m.rawJsonString = oRequest.Form("examformjson")
m.rawJsonObj= m.oJSON.Parse(m.rawJsonString)
IF VARTYPE(m.rawJsonObj) = 'O'
    *-- parsing goes here
ENDIF 

Coordinator
Oct 17, 2013 at 11:33 PM
Edited Oct 17, 2013 at 11:38 PM
json2 in NEWOBJECT is basically CraigBoyd's code

BTW: The json codes are working on version 553. I haven't migrated this project to the latest version.
Developer
Oct 18, 2013 at 4:02 PM
Hi apaustria,

Great. I could see why I was having this pesky problem, on first line of your mail.

I was using $('form.ajax') instead of $('#examforms'). I keep forgetting 'mobile' part of 'JQuery mobile'. ..And, as usual, I first blame AVFP.

Interesting use of serializeObject(). Is this something you wrote? If you share it then, I'll definitely use it.

Currently, our server side 'submit handler', needs to work with or without javascript enabled, and expects same values of oRequest.form() in both instances, but I am open to improvement.

Regarding Json, I've only changed the Json.prg back to it's original. the rest is the same. I can't report any error from this changes, till now.

Haven't checked oJSON.parse() though.

Thanks.
Coordinator
Oct 18, 2013 at 5:40 PM
Edited Oct 18, 2013 at 5:49 PM
I didn't originally wrote the code, can't remember though where I got it (stackoverflow maybe), but this does the job for me. :)
<script>    
    $.fn.serializeObject = function()
    {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
</script>
I think it's originally from here:
http://stackoverflow.com/questions/8900587/jquery-serializeobject-is-not-a-function-only-in-firefox
Developer
Oct 18, 2013 at 10:31 PM
Yep,

I did see that one, but I ignored, it since it was not part of CDNs and was not sure if you meant the same function. I must say, you had a very unique way of using it.

Now I wish we could get all form variables that way form HTTPs reqst. It'll be just like using TableName.FieldName. very nice.

Thanks.
Coordinator
Oct 20, 2013 at 3:07 PM
Now I wish we could get all form variables that way form HTTPs reqst. It'll be just like using TableName.FieldName. very nice.

Thanks.
We've been able to do this for a while:
* Note the use of oRequest.oRequest to get these variables
FOR EACH lcFormVar IN oRequest.oRequest.FORM
    lcVar =   oRequest.oRequest.FORM(lcFormVar).ITEM()
    lcSTR=lcSTR+[    <TR><TD><b> ]+ lcFormVar +[ </b></TD><TD> ] ;
        +lcVar +[</TD></TR>] 
NEXT
Anything you can do in Classic ASP, you can do in AVFP, plus all the great things FoxPro does!
Developer
Oct 22, 2013 at 12:07 AM
Good point claudefox.
Thanks.