Harbour Dialect : reading/writing to JSON files?

This forum is meant for questions and discussions about the X# language and tools
Post Reply
RolWil
Posts: 68
Joined: Mon Jul 18, 2022 3:16 am

Harbour Dialect : reading/writing to JSON files?

Post 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!
User avatar
wriedmann
Posts: 3748
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Harbour Dialect : reading/writing to JSON files?

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
RolWil
Posts: 68
Joined: Mon Jul 18, 2022 3:16 am

Harbour Dialect : reading/writing to JSON files?

Post 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.
User avatar
wriedmann
Posts: 3748
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Harbour Dialect : reading/writing to JSON files?

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
RolWil
Posts: 68
Joined: Mon Jul 18, 2022 3:16 am

Harbour Dialect : reading/writing to JSON files?

Post 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'"
User avatar
Chris
Posts: 4845
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Harbour Dialect : reading/writing to JSON files?

Post 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.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
RolWil
Posts: 68
Joined: Mon Jul 18, 2022 3:16 am

Harbour Dialect : reading/writing to JSON files?

Post 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"
}
User avatar
wriedmann
Posts: 3748
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Harbour Dialect : reading/writing to JSON files?

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4845
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Harbour Dialect : reading/writing to JSON files?

Post 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.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
RolWil
Posts: 68
Joined: Mon Jul 18, 2022 3:16 am

Harbour Dialect : reading/writing to JSON files?

Post 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".
Post Reply