It is currently Sun Dec 15, 2019 3:53 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: How to store a JSON string in a DataSection?
PostPosted: Thu May 30, 2019 10:59 pm 
Offline
Always Here
Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 5890
Location: Wales, UK
JSON strings include {:[", etc, so how can that be encapsulated in a DataSection?

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Top
 Profile  
Reply with quote  
 Post subject: Re: How to store a JSON string in a DataSection?
PostPosted: Thu May 30, 2019 11:15 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Mar 07, 2004 8:47 am
Posts: 1857
Location: Argentina
Hi, you could try using EscapeString() and UnescapeString() for one way of doing it.

Code:
EnableExplicit

Structure TEST_STRUCT
   DecimalPlaces.i
   WindowRect.RECT
EndStructure

Define.s json_raw
Define.s json_unescaped
Define.TEST_STRUCT struct

; obtain the raw escaped data.
Restore json_data
Read.s json_raw

json_unescaped = UnescapeString(json_raw, #PB_String_EscapeXML) ; unescape the string data

Define.i json = ParseJSON(#PB_Any, json_unescaped) ; parse the unescaped string
If IsJSON( json ) ; did we get a valid json?
  ExtractJSONStructure(JSONValue(json), @struct, TEST_STRUCT) ; load up the data onto the structure
  FreeJSON( json ) ; get rid of the jeeey, son.
EndIf

;Show me what you've got!
Debug json_raw
Debug "-------"
Debug json_unescaped
Debug "-------"
Debug "Decimal places: " + struct\DecimalPlaces
Debug "Rect Left: " + struct\WindowRect\left
Debug "Rect Bottom: " + struct\WindowRect\bottom
Debug "Rect Top: " + struct\WindowRect\top
Debug "Rect Right: " + struct\WindowRect\right

DataSection
   json_data:
   Data.s "{"WindowRect":{"left":12,"bottom":34,"top":56,"right":78},"DecimalPlaces":1234}"
   


One way you could quickly escape the text for testing purposes (or for a once off) is to use GetClipboardText()

Code:
Debug EscapeString( GetClipboardText(), #PB_String_EscapeXML)


Cheers!

_________________
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)


Top
 Profile  
Reply with quote  
 Post subject: Re: How to store a JSON string in a DataSection?
PostPosted: Fri May 31, 2019 12:16 am 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3595
Location: Utah, USA
You could store the JSON string in a file and use IncludeBinary "D:\file" in the DataSection.

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: How to store a JSON string in a DataSection?
PostPosted: Fri May 31, 2019 2:12 am 
Offline
Always Here
Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 5890
Location: Wales, UK
Hi dagcrack

Thanks for your reply.
It's not possible to store JSON strings in the Data Section like that if they include symbols like speech marks, hence my post. I was hoping there could be a clever way to encapsulate the whole JSON string such that PB sees it as just another string.
I'm not going to parse the JSON strings, they are part of a set of info that is saved in a text file.

My current solution is the read them in from a file, concern being that they could be inadvertently deleted - but Demivec's idea solves that.

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Top
 Profile  
Reply with quote  
 Post subject: Re: How to store a JSON string in a DataSection?
PostPosted: Fri May 31, 2019 2:14 am 
Offline
Always Here
Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 5890
Location: Wales, UK
Hi Demivec

Quote:
You could store the JSON string in a file and use IncludeBinary "D:\file" in the DataSection.


... great idea, thank you.

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Top
 Profile  
Reply with quote  
 Post subject: Re: How to store a JSON string in a DataSection?
PostPosted: Sun Jun 09, 2019 11:36 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Mar 07, 2004 8:47 am
Posts: 1857
Location: Argentina
Speech marks? try using Base64 encoding then. If for any reason you don't want the resource to be edited it would at least make it "slightly" harder to do so; although it is fairly trivial.

_________________
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye