Topic
  • 4 replies
  • Latest Post - ‏2012-04-16T14:26:43Z by RaviM
RaviM
RaviM
59 Posts

Pinned topic Impact 5: Using Java IPL to convert json string

‏2012-04-12T14:52:34Z |
Hi: I need some help with converting a json string into variables within a policy using the Java IPL functions. I have added the google-gson-2.1.jar to Impact runtime. I am quite not sure the arguments for the fromJson method in the JavaCall. Can someone help. This is what have so far.

code
jsonString = '{ "ids": }';

gson = NewJavaObject("com.google.gson.GsonBuilder", {});
JavaCall(null, gson, "create", {});
JavaCall(??, gson, "fromJson", {jsonString, ??});
[/code]
Updated on 2012-04-16T14:26:43Z at 2012-04-16T14:26:43Z by RaviM
  • RaviM
    RaviM
    59 Posts

    Re: Impact 5: Using Java IPL to convert json string

    ‏2012-04-12T14:55:51Z  
    Website is formatting the jsonString. Reposting the code I have so far.
    jsonString = '{ "ids": }';

    gson = NewJavaObject("com.google.gson.GsonBuilder", {});
    JavaCall(null, gson, "create", {});
    JavaCall(??, gson, "fromJson", {jsonString, ??});
  • ivb
    ivb
    8 Posts

    Re: Impact 5: Using Java IPL to convert json string

    ‏2012-04-12T15:06:55Z  
    Looking at the API, this is going to be harder to do than I initially thought..

    It looks like you will have to write classes to hold the results of the fromJSON method, bundle the classes into a JAR and load it into your Impact EAR.. Then in your policy use the reflection API to get Class values.. Yea, its going to suck.

    Can you upgrade to Impact 6.1 and use Javascript policies? I have checked but JSON should be built in..

    If you cant go to 6.1 I guess you will have to regex or tokenize strings to build IPL objects.
    I.V. Blankenship
    Gulf Breeze Software
    http://www.gulfsoft.com
  • ivb
    ivb
    8 Posts

    Re: Impact 5: Using Java IPL to convert json string

    ‏2012-04-14T19:04:04Z  
    OK, I was able to get a JSON string parsed using the JSON Java classes from https://github.com/douglascrockford/JSON-java.
    I had to make a project in Eclipse and export a JAR using J2SE-1.5 compliance.

    You can find the API Docs here http://www.json.org/java/

    Here is a simple test policy:
    
    handle java.lang.Exception 
    { log(CurrentContext()); 
    }   jsonString=
    "{\"glossary\": {\"title\": \"example glossary\",\"GlossDiv\": {\"title\": \"S\",\"GlossList\": {\"GlossEntry\": {\"ID\": \"SGML\",\"SortAs\": \"SGML\",\"GlossTerm\": \"Standard Generalized Markup Language\",\"Acronym\": \"SGML\",\"Abbrev\": \"ISO 8879:1986\",\"GlossDef\": {\"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\"GlossSeeAlso\": [\"GML\", \"XML\"]},\"GlossSee\": \"markup\"}}}}}";   jsonTokener=NewJavaObject(
    "org.json.JSONTokener", 
    {jsonString
    }); 
    
    if(jsonTokener != Null) 
    { jsonObject=NewJavaObject(
    "org.json.JSONObject", 
    {jsonTokener
    }); 
    
    if(jsonObject == Null) 
    { Exit(); 
    } 
    } 
    
    else 
    { Exit(); 
    }   log(JavaCall(Null,jsonObject,
    "toString",
    {1
    }));   names=JavaCall(Null,jsonObject,
    "names",
    {
    }); log(names);
    


    And here are the results:
    
    14 Apr 2012 14:53:03,830: [JSON_Test][pool-2-thread-14]Parser log: 
    {
    "glossary": 
    { 
    "GlossDiv": 
    { 
    "title": 
    "S", 
    "GlossList": 
    {
    "GlossEntry": 
    { 
    "Abbrev": 
    "ISO 8879:1986", 
    "Acronym": 
    "SGML", 
    "ID": 
    "SGML", 
    "GlossSee": 
    "markup", 
    "GlossTerm": 
    "Standard Generalized Markup Language", 
    "GlossDef": 
    { 
    "para": 
    "A meta-markup language, used to create markup languages such as DocBook.", 
    "GlossSeeAlso": [ 
    "GML", 
    "XML" ] 
    }, 
    "SortAs": 
    "SGML" 
    }
    } 
    }, 
    "title": 
    "example glossary" 
    }
    } 14 Apr 2012 14:53:03,830: [JSON_Test][pool-2-thread-14]Parser log: [
    "glossary"] 14 Apr 2012 14:53:03,830: [JSON_Test][pool-2-thread-14]!JSON_Test execution took 4ms
    


    I.V. Blankenship
    Gulf Breeze Software
    http://www.gulfsoft.com
  • RaviM
    RaviM
    59 Posts

    Re: Impact 5: Using Java IPL to convert json string

    ‏2012-04-16T14:26:43Z  
    • ivb
    • ‏2012-04-14T19:04:04Z
    OK, I was able to get a JSON string parsed using the JSON Java classes from https://github.com/douglascrockford/JSON-java.
    I had to make a project in Eclipse and export a JAR using J2SE-1.5 compliance.

    You can find the API Docs here http://www.json.org/java/

    Here is a simple test policy:
    <pre class="jive-pre"> handle java.lang.Exception { log(CurrentContext()); } jsonString= "{\"glossary\": {\"title\": \"example glossary\",\"GlossDiv\": {\"title\": \"S\",\"GlossList\": {\"GlossEntry\": {\"ID\": \"SGML\",\"SortAs\": \"SGML\",\"GlossTerm\": \"Standard Generalized Markup Language\",\"Acronym\": \"SGML\",\"Abbrev\": \"ISO 8879:1986\",\"GlossDef\": {\"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\"GlossSeeAlso\": [\"GML\", \"XML\"]},\"GlossSee\": \"markup\"}}}}}"; jsonTokener=NewJavaObject( "org.json.JSONTokener", {jsonString }); if(jsonTokener != Null) { jsonObject=NewJavaObject( "org.json.JSONObject", {jsonTokener }); if(jsonObject == Null) { Exit(); } } else { Exit(); } log(JavaCall(Null,jsonObject, "toString", {1 })); names=JavaCall(Null,jsonObject, "names", { }); log(names); </pre>

    And here are the results:
    <pre class="jive-pre"> 14 Apr 2012 14:53:03,830: [JSON_Test][pool-2-thread-14]Parser log: { "glossary": { "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "Abbrev": "ISO 8879:1986", "Acronym": "SGML", "ID": "SGML", "GlossSee": "markup", "GlossTerm": "Standard Generalized Markup Language", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": [ "GML", "XML" ] }, "SortAs": "SGML" } } }, "title": "example glossary" } } 14 Apr 2012 14:53:03,830: [JSON_Test][pool-2-thread-14]Parser log: [ "glossary"] 14 Apr 2012 14:53:03,830: [JSON_Test][pool-2-thread-14]!JSON_Test execution took 4ms </pre>

    I.V. Blankenship
    Gulf Breeze Software
    http://www.gulfsoft.com
    Thank you so much. I will give this a try and let you know.