Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ampersand for "label" #10

Open
HemulGM opened this issue Jan 15, 2020 · 30 comments
Open

Ampersand for "label" #10

HemulGM opened this issue Jan 15, 2020 · 30 comments

Comments

@HemulGM
Copy link

HemulGM commented Jan 15, 2020

I use the generator on the site.

When generating a field with the name "label", an ampersand is necessary like with the field "type".

@JensBorrisholt
Copy link

@HemulGM have a look at my branch https://github.com/JensBorrisholt/Delphi-JsonToDelphiClass

I've fixed that there.

Regards

Jens Borrisholt

@JACAmetekDenmark
Copy link

JACAmetekDenmark commented Oct 23, 2023

New with JSON and Delphi, piece of cake with VS C# , however this is becoming a nightmare in delphi.
The & causes a small "sET" in my generated file. Must be a "SET" to match the label when reading from .dot module.
Is there a simple way around it.
TJsonDTO is great to read an existing JSON file but a pain to generate. Is there a function to extract the contents into a TStringList so I can pretty-write to a file?
Thanks in advance
James

@HemulGM
Copy link
Author

HemulGM commented Oct 23, 2023

New with JSON and Delphi, piece of cake with VS C# , however this is becoming a nightmare in delphi. The & causes a small "sET" in my generated file. Must be a "SET" to match the label when reading from .dot module. Is there a simple way around it. TJsonDTO is great to read an existing JSON file but a pain to generate. Is there a function to extract the contents into a TStringList so I can pretty-write to a file? Thanks in advance James

JSON and its mechanics have nothing to do with it. This is a reserved word and there are exactly the same problems with them in C#.

@JACAmetekDenmark
Copy link

Thanks for the quick reply. Protocol and communications DLL (to the device) are developed in C# with no conflicts, it's only now a problem with reserved word clashes in Delphi, when integrating the host systems developed in Delphi. It never occurred to me it would be an issue with delphi.
Thanks anyway.

@HemulGM
Copy link
Author

HemulGM commented Oct 23, 2023

Thanks for the quick reply. Protocol and communications DLL (to the device) are developed in C# with no conflicts, it's only now a problem with reserved word clashes in Delphi, when integrating the host systems developed in Delphi. It never occurred to me it would be an issue with delphi. Thanks anyway.

No one is stopping you from not using reserved words at all. Delphi serialization, like everywhere else, works with attributes, through which you can specify any field name from JSON for a field in a class.

@HemulGM
Copy link
Author

HemulGM commented Oct 23, 2023

  TCategoryScores = class
  private
    FHate: Extended;
    [JSONName('hate/threatening')]
    FHatethreatening: Extended;
    [JSONName('self-harm')]
    FSelfharm: Extended;
    FSexual: Extended;
    [JSONName('sexual/minors')]
    FSexualminors: Extended;
    FViolence: Extended;
    [JSONName('violence/graphic')]
    FViolencegraphic: Extended;
    FHarassment: Extended;
    [JSONName('harassment/threatening')]
    FHarassmentthreatening: Extended;
    [JSONName('self-harm/intent')]
    FSelfharmintent: Extended;
    [JSONName('self-harm/instructions')]
    FSelfharminstructions: Extended;
  public
    /// <summary>
    /// Content that expresses, incites, or promotes hate based on race, gender, ethnicity, religion, nationality,
    /// sexual orientation, disability status, or caste. Hateful content aimed at non-protected groups (e.g., chess players)
    /// is harrassment.
    /// </summary>
    property Hate: Extended read FHate write FHate;
    /// <summary>
    /// Hateful content that also includes violence or serious harm towards the targeted group based on race,
    /// gender, ethnicity, religion, nationality, sexual orientation, disability status, or caste.
    /// </summary>
    property HateOrThreatening: Extended read FHatethreatening write FHatethreatening;
    /// <summary>
    /// Content that expresses, incites, or promotes harassing language towards any target.
    /// </summary>
    property Harassment: Extended read FHarassment write FHarassment;
    /// <summary>
    /// Harassment content that also includes violence or serious harm towards any target.
    /// </summary>
    property HarassmentOrThreatening: Extended read FHarassmentthreatening write FHarassmentthreatening;
    /// <summary>
    /// Content that promotes, encourages, or depicts acts of self-harm, such as suicide, cutting, and eating disorders.
    /// </summary>
    property SelfHarm: Extended read FSelfharm write FSelfharm;
    /// <summary>
    /// Content where the speaker expresses that they are engaging or intend to engage in acts of self-harm,
    /// such as suicide, cutting, and eating disorders.
    /// </summary>
    property SelfHarmOrIntent: Extended read FSelfharmintent write FSelfharmintent;
    /// <summary>
    /// Content that encourages performing acts of self-harm, such as suicide, cutting, and eating disorders,
    /// or that gives instructions or advice on how to commit such acts.
    /// </summary>
    property SelfHarmOrInstructions: Extended read FSelfharminstructions write FSelfharminstructions;
    /// <summary>
    /// Content meant to arouse sexual excitement, such as the description of sexual activity,
    /// or that promotes sexual services (excluding sex education and wellness).
    /// </summary>
    property Sexual: Extended read FSexual write FSexual;
    /// <summary>
    /// Sexual content that includes an individual who is under 18 years old.
    /// </summary>
    property SexualOrMinors: Extended read FSexualminors write FSexualminors;
    /// <summary>
    /// Content that depicts death, violence, or physical injury.
    /// </summary>
    property Violence: Extended read FViolence write FViolence;
    /// <summary>
    /// Content that depicts death, violence, or physical injury in graphic detail.
    /// </summary>
    property ViolenceOrGraphic: Extended read FViolencegraphic write FViolencegraphic;
  end;

@JensBorrisholt
Copy link

Hi @JACAmetekDenmark

I'm not exactly sure what the issue are. Could you provide me with an example?

Just send it direct to [email protected]

@JACAmetekDenmark
Copy link

JACAmetekDenmark commented Oct 24, 2023 via email

@JACAmetekDenmark
Copy link

thanks for the heads up on attributes
Is there a help source I can refer to before spamming you with questions? Have 2
which is the correct method to set a value?
AddWorkOrder.AsJson:= '{ "SET":"AddWorkOrder" } ';
&SET:='AddWorkOrder';
Best method to write to a file.
TFile.WriteAllText('C:\Ametek\RTCRefresh\DelphiAddworker.JSON',AddWorkOrder.AsJson, TEncoding.UTF8);
or
Empty to TStringList then write to file?
Apologies for the beginner questions.
regards
James

@HemulGM
Copy link
Author

HemulGM commented Oct 24, 2023

https://docwiki.embarcadero.com/Libraries/Alexandria/en/System.JSON.Serializers.JsonNameAttribute

{
   "SET":"AddWorkOrder",
   "TestType":"Calibration",
   "WO_GUID":"",
   "ID":"copy Temp pt100",
   "CompanyName":"Ametek Denmark"
}
TAddWorkOrder = class
private
  FCompanyName: String;
  FID: String;
  FSET: String;
  FTestType: String;
  FWO_GUID: String;
public
  property CompanyName: String read FCompanyName write FCompanyName;
  property ID: String read FID write FID;
  property AnyFieldName: String read FSET write FSET;
  property TestType: String read FTestType write FTestType;
  property WO_GUID: String read FWO_GUID write FWO_GUID;
end;
  TJson.JsonToObject<TAddWorkOrder>(YouJsonText);

@JACAmetekDenmark
Copy link

Thankyou, have applied the attribute to name the component and that part is solved. However is your example manually defined or result of the generator ? if I run that short JSON text through the generator I still get a Property " &SET"

@HemulGM
Copy link
Author

HemulGM commented Oct 24, 2023

Thankyou, have applied the attribute to name the component and that part is solved. However is your example manually defined or result of the generator ? if I run that short JSON text through the generator I still get a Property " &SET"

I don’t see any difficulty in correcting such fields to be acceptable. Because the property name does not affect serialization

@JACAmetekDenmark
Copy link

JACAmetekDenmark commented Oct 24, 2023

you haven't defined the SET component which has to be published, in your example. That is the issue.

@HemulGM
Copy link
Author

HemulGM commented Oct 24, 2023

you haven't defined the SET component which has to be published, in your example. That is the issue.

It's not a mistake. And it will work. Check

@JACAmetekDenmark
Copy link

I think you misunderstood me. I need to generate the file ... not load it. How do I set "SET" = "AddWorkorder" if there is no public access?

@JACAmetekDenmark
Copy link

Name attribute does not work on arrays,
[JSONName('Sensors'), JSONMarshalled(False)]
FSensorsArray: TArray;
[GenericListReflect]
FSensors: TObjectList;

The node in the JSON file is always "sensors"

@HemulGM
Copy link
Author

HemulGM commented Oct 24, 2023

I think you misunderstood me. I need to generate the file ... not load it. How do I set "SET" = "AddWorkorder" if there is no public access?

image

@HemulGM
Copy link
Author

HemulGM commented Oct 24, 2023

You can set ANY property name. Its name will be taken from the private field!

@JACAmetekDenmark
Copy link

Apologies, i was only looking for the SET in the properties list, didn't see the AnyFieldName.
Is there an issue with the Name attributes when applied to arrays?

Name attribute does not work on arrays,

[JSONName('Sensors'), JSONMarshalled(False)]
FSensorsArray: TArray;
[GenericListReflect]
FSensors: TObjectList;

The node in the JSON file is always "sensors"

@HemulGM
Copy link
Author

HemulGM commented Oct 24, 2023

image

@JACAmetekDenmark
Copy link

Thankyou. strange both JSONName and JSONNameAttributey worked after I assigned the attributes to both the TArray and the TObjectList.

[JSONNameAttribute('Sensors'), JSONMarshalled(False)]
FSensorsArray: TArray;
[JSONNameAttribute('Sensors'), GenericListReflect]
FSensors: TObjectList;

Very much appreciate your patience and help.
All seems to work with JSON which will save me alot of time to finding another solution.
regards
James

@JensBorrisholt
Copy link

@JACAmetekDenmark I'm glad you got help. I was busy yesterday. You also spoked about "Is there a function to extract the contents into a TStringList so I can pretty-write to a file?"

Dig you got helt with that aswell? Or do you need a demo application.

@JACAmetekDenmark
Copy link

No we haven't touched on that.
I use " TFile.WriteAllText('C:\Ametek\RTCRefresh\DelphiAddworker.JSON',AddWorkOrder.AsJson, TEncoding.UTF8);"
to write to the file which works however unformatted and without indentation.
So a demo or advice would be apprecated.
regards
James

@JensBorrisholt
Copy link

@JACAmetekDenmark can I have a copy of the file? Send it in a email, please, and I'll make you a demo.

@HemulGM
Copy link
Author

HemulGM commented Oct 25, 2023

No we haven't touched on that. I use " TFile.WriteAllText('C:\Ametek\RTCRefresh\DelphiAddworker.JSON',AddWorkOrder.AsJson, TEncoding.UTF8);" to write to the file which works however unformatted and without indentation. So a demo or advice would be apprecated. regards James

var JSON := TJSON.ObjectToJsonObject(YourObject);
try
  TFile.WriteAllText('C:\Ametek\RTCRefresh\DelphiAddworker.JSON', JSON.Format, TEncoding.UTF8);
finally
  JSON.Free;
end;

@JensBorrisholt
Copy link

@JACAmetekDenmark now I'm confused: Do you want to pretty print a JSON file (format with indentenration) or do you want do create json from a object and save that to a file?

For the latter
Create (or generate) a unis that inheritets from TJsonDTO
on you TJsonDTO decentant you have a property called AsJson, save that to at file.

TJSON.ObjectToJsonObject is the build in functioanllity, and have nothing to do with mit repo

@JensBorrisholt
Copy link

It seems like you are not using TJsonDTO in your exampels at all and thats where your problems starts

@JACAmetekDenmark
Copy link

Hi Jens, i inherit from TJsonDTO . here us a snippet.

TAddWorkOrder = class(TJsonDTO)
private
[JSONName('SET')]
FSET: string;
FTestType: string;
[JSONName('WO_GUID')]
FWOGUId: string;
[JSONName('ID')]
FId: string;
[JSONName('Sensors'), JSONMarshalled(False)]
FSensorsArray: TArray;
[JSONName('Sensors'), GenericListReflect]
FSensors: TObjectList;
[JSONName('Scenario'), JSONMarshalled(False)]

[JSONName('ProcedureInfo')]
FProcedureInfo: TProcedureInfo;
function GetSensors: TObjectList<TSensors>;
function GetSibInputPorts: TObjectList<TSibInputPorts>;

protected
function GetAsJson: string; override;
published
property &SET: string read FSET write FSET;
property TestType: string read FTestType write FTestType;
property Calibrator: TCalibrator read FCalibrator;

Regarding JSON file
I want to simply save the AddWorkOrder json object to a JSON file in JSON format.

@JensBorrisholt
Copy link

@JACAmetekDenmark

I havent had time to look make sure the result are always formated but here is a smalle function for format a JSON file

function FormatJSON(json: String): String;
var
tmpJson: TJSONObject;
begin
tmpJson := TJSONObject.ParseJSONValue(json) as TJSONObject;
Result := TJson.Format(tmpJson);
FreeAndNil(tmpJson);
end;

@HemulGM
Copy link
Author

HemulGM commented Oct 28, 2023

@JACAmetekDenmark

I havent had time to look make sure the result are always formated but here is a smalle function for format a JSON file

function FormatJSON(json: String): String;
var
tmpJson: TJSONObject;
begin
tmpJson := TJSONObject.ParseJSONValue(json) as TJSONObject;
Result := TJson.Format(tmpJson);
FreeAndNil(tmpJson);
end;

Just TJsonValue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants