Harbour Dialect : reading/writing to JSON files?
Harbour Dialect : reading/writing to JSON files?
Hi All,
I'm Slugging my way through my Harbour -> XSharp conversion.
Does anyone have sample code for reading/writing JSON files? Note: this is in the Harbour dialect.
Thanks in Aadvance!
I'm Slugging my way through my Harbour -> XSharp conversion.
Does anyone have sample code for reading/writing JSON files? Note: this is in the Harbour dialect.
Thanks in Aadvance!
Harbour Dialect : reading/writing to JSON files?
Hi Roland,
the .NET Framework has some powerful JSON processing libraries.
You can do it with both the Microsoft tools (I'm using them) or with the NewtonSoft version.
You can start here:
https://learn.microsoft.com/en-us/dotne ... n/overview
I have opted for Microsofts implementation - it works really well if you are able to build the right classes for it.
If you like to go that direction please let me know.
Wolfgang
the .NET Framework has some powerful JSON processing libraries.
You can do it with both the Microsoft tools (I'm using them) or with the NewtonSoft version.
You can start here:
https://learn.microsoft.com/en-us/dotne ... n/overview
I have opted for Microsofts implementation - it works really well if you are able to build the right classes for it.
If you like to go that direction please let me know.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Harbour Dialect : reading/writing to JSON files?
Thanks Wolfgang.
I’ve use Newtonsoft quite a bit in a few VS C# .net applications.
Here’s a bit of code how far I’ve gotten in Harbour/XSharp in a VS XSharp form:
JSON Data :
Inspecting “parsedJSON” during executionshows it to be type object with the following content:
In ‘regular’ C#, I set up classes to map the JSON data, but don’t know how to do this in XSharp.
My hope was to reference the value via the name, for example:
but I get a “ 'parsedJSON' is a variable but is used like a type” error.
I'd prefer to go with classes, but don't know how to set them up in a XSharp/Harbour VS project.
I’ve use Newtonsoft quite a bit in a few VS C# .net applications.
Here’s a bit of code how far I’ve gotten in Harbour/XSharp in a VS XSharp form:
Code: Select all
jsonIn := FReadStr(ptrHandle,99999)
parsedJSON := Newtonsoft.Json.Linq.JObject.Parse(jsonIn)
ntaxRate := parsedJSON.SelectToken("TaxRate")
Code: Select all
{
"TaxRate": "1.08",
"Premium":"1.45"
}
Code: Select all
{{
"TaxRate": "1.08",
"LI6Prem": "1.45"}}
My hope was to reference the value via the name, for example:
Code: Select all
ntaxRate := parsedJSON.SelectToken("TaxRate")
I'd prefer to go with classes, but don't know how to set them up in a XSharp/Harbour VS project.
Harbour Dialect : reading/writing to JSON files?
Hi Roland,
the beauty of X# is that you can mix xBase code with C# code.
If you know how to write the code in C#, then you can do it in X#.
Your code
should be written as
because parsedJSON is an object, and you are calling a method of this object, and not a method of the class.
Wolfgang
the beauty of X# is that you can mix xBase code with C# code.
If you know how to write the code in C#, then you can do it in X#.
Your code
Code: Select all
ntaxRate := parsedJSON.SelectToken("TaxRate")
Code: Select all
ntaxRate := parsedJSON:SelectToken("TaxRate")
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Harbour Dialect : reading/writing to JSON files?
Thanks Wolfgang;
I had tried that, but "ntaxRate" ends up being an object; for example, I tried to display it using:
but get a runtime error "'Conversion Error from USUAL (OBJECT) to STRING'"
I had tried that, but "ntaxRate" ends up being an object; for example, I tried to display it using:
Code: Select all
MessageBox.Show(ntaxRate)
Harbour Dialect : reading/writing to JSON files?
Hi Roland,
I think it's better if you show the complete code, including how the vars are defined, so it is easier to give you better help.
I think it's better if you show the complete code, including how the vars are defined, so it is easier to give you better help.
Chris Pyrgas
XSharp Development Team
chris(at)xsharp.eu
XSharp Development Team
chris(at)xsharp.eu
Harbour Dialect : reading/writing to JSON files?
Hi Chris, thanks.
The form only has a button; the MessageBox is where it trips up:
Content of JSON file:
The form only has a button; the MessageBox is where it trips up:
Code: Select all
USING System
USING System.Collections.Generic
USING System.ComponentModel
USING System.Data
USING System.Drawing
USING System.Linq
USING System.Text
USING System.Threading.Tasks
USING System.Windows.Forms
BEGIN NAMESPACE WindowsFormsApplication10___test_json
PUBLIC PARTIAL CLASS Form1 ;
INHERIT System.Windows.Forms.Form
PUBLIC CONSTRUCTOR() STRICT//Form1
InitializeComponent()
RETURN
END CONSTRUCTOR
PRIVATE METHOD button1_Click(sender AS System.Object, e AS System.EventArgs) AS VOID STRICT
LOCAL ptrHandle
LOCAL jsonIn
LOCAL parsedJSON
LOCAL ntaxRate
// read in JSON file:
ptrHandle := FOpen("OSSdedParms.json")
jsonIn := FReadStr(ptrHandle,99999)
FClose(ptrHandle)
//
parsedJSON := Newtonsoft.Json.Linq.JObject.Parse(jsonIn)
ntaxRate := parsedJSON:SelectToken("TaxRate")
MessageBox.Show(ntaxRate) // <---- Error "Conversion Error from USUAL (OBJECT) to STRING"
RETURN
END METHOD
END CLASS
END NAMESPACE
Code: Select all
{
"TaxRate": "1.08",
"LI6Prem":"1.45"
}
Harbour Dialect : reading/writing to JSON files?
Hi Roland,
xBase permits untyped variables, but if you are writing new code it is better to type your variables.
As you know from C#, methods are not forgiving variable error types.
Therefore you should write
Even if ntaxRate would be a numeric value, the .NET runtime would throw an exception.
The easiest way to check that would be to debug this code adding an Altd() call before the messageBox call and inspecting the variable content and type.
Wolfgang
xBase permits untyped variables, but if you are writing new code it is better to type your variables.
As you know from C#, methods are not forgiving variable error types.
Therefore you should write
Code: Select all
MessageBox.Show(ntaxRate:ToString())
The easiest way to check that would be to debug this code adding an Altd() call before the messageBox call and inspecting the variable content and type.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Harbour Dialect : reading/writing to JSON files?
Hi Roland,
To add to what Wolfgang said, I would type the variables like:
LOCAL jsonIn AS STRING
LOCAL parsedJSON AS Newtonsoft.Json.Linq.JObject
LOCAL ntaxRate AS Newtonsoft.Json.Linq.JToken
which will also help you by providing editor intellisense on your code.
In general, if you have c# code for anything, you can translate it 1:1 to X#, using the same locals, types, methods etc.
To add to what Wolfgang said, I would type the variables like:
LOCAL jsonIn AS STRING
LOCAL parsedJSON AS Newtonsoft.Json.Linq.JObject
LOCAL ntaxRate AS Newtonsoft.Json.Linq.JToken
which will also help you by providing editor intellisense on your code.
In general, if you have c# code for anything, you can translate it 1:1 to X#, using the same locals, types, methods etc.
Chris Pyrgas
XSharp Development Team
chris(at)xsharp.eu
XSharp Development Team
chris(at)xsharp.eu
Harbour Dialect : reading/writing to JSON files?
Thanks for everyone's help! It is greatly appreciated.
I'm now back at it after some time on other projects. I'm past the reading and writing to JSON files and moving on. Unfortunately, VS has now thrown a wrench in my progress. I'll start a new topic on this issue: "The designer could not be shown for this file because none of the classes within it can be designed".
I'm now back at it after some time on other projects. I'm past the reading and writing to JSON files and moving on. Unfortunately, VS has now thrown a wrench in my progress. I'll start a new topic on this issue: "The designer could not be shown for this file because none of the classes within it can be designed".