xsharp.eu • Harbour Dialect : reading/writing to JSON files?
Page 1 of 1

Harbour Dialect : reading/writing to JSON files?

Posted: Wed Apr 19, 2023 2:44 am
by RolWil
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!

Harbour Dialect : reading/writing to JSON files?

Posted: Wed Apr 19, 2023 3:39 am
by wriedmann
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

Harbour Dialect : reading/writing to JSON files?

Posted: Wed Apr 19, 2023 5:15 pm
by RolWil
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:

Code: Select all

jsonIn := FReadStr(ptrHandle,99999)
parsedJSON :=  Newtonsoft.Json.Linq.JObject.Parse(jsonIn)                   
ntaxRate := parsedJSON.SelectToken("TaxRate")
JSON Data :

Code: Select all

{
    "TaxRate": "1.08",
    "Premium":"1.45"
}
Inspecting “parsedJSON” during executionshows it to be type object with the following content:

Code: Select all

{{
  "TaxRate": "1.08",
  "LI6Prem": "1.45"}}
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:

Code: Select all

ntaxRate := parsedJSON.SelectToken("TaxRate")
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.

Harbour Dialect : reading/writing to JSON files?

Posted: Wed Apr 19, 2023 6:02 pm
by wriedmann
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

Code: Select all

ntaxRate := parsedJSON.SelectToken("TaxRate")
should be written as

Code: Select all

ntaxRate := parsedJSON:SelectToken("TaxRate")
because parsedJSON is an object, and you are calling a method of this object, and not a method of the class.

Wolfgang

Harbour Dialect : reading/writing to JSON files?

Posted: Wed Apr 19, 2023 6:20 pm
by RolWil
Thanks Wolfgang;

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)
but get a runtime error "'Conversion Error from USUAL (OBJECT) to STRING'"

Harbour Dialect : reading/writing to JSON files?

Posted: Wed Apr 19, 2023 7:08 pm
by Chris
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.

Harbour Dialect : reading/writing to JSON files?

Posted: Thu Apr 20, 2023 2:48 am
by RolWil
Hi Chris, thanks.

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
Content of JSON file:

Code: Select all

{
    "TaxRate": "1.08",
    "LI6Prem":"1.45"
}

Harbour Dialect : reading/writing to JSON files?

Posted: Thu Apr 20, 2023 4:18 am
by wriedmann
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

Code: Select all

MessageBox.Show(ntaxRate:ToString())
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

Harbour Dialect : reading/writing to JSON files?

Posted: Thu Apr 20, 2023 6:19 am
by Chris
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.

Harbour Dialect : reading/writing to JSON files?

Posted: Fri Apr 28, 2023 3:21 am
by RolWil
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".