AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

"F"-Prefix beim TJSONUnMarshal

Ein Thema von Der schöne Günther · begonnen am 1. Okt 2014 · letzter Beitrag vom 2. Okt 2014
 
Der schöne Günther

Registriert seit: 6. Mär 2013
6.190 Beiträge
 
Delphi 10 Seattle Enterprise
 
#1

"F"-Prefix beim TJSONUnMarshal

  Alt 1. Okt 2014, 12:19
Delphi-Version: 5
Ich wollte grade ganz unbedarft in die Materie einsteigen. Ich weiß nichts über JSON. XE7.

Embarcaderos Blog-Beitrag "How to convert an object to JSON and back with a single line of code" sah hier schon sehr vielversprechend aus.


Folgendes Beispiel:

Project1.DPR
Delphi-Quellcode:
program Project1;

{$APPTYPE CONSOLE}
{$R *.res}

uses
   System.SysUtils,
   System.JSon,
   Rest.Json,
   MyTypeUnit in 'MyTypeUnit.pas';

var
   jsonObj: TJSONObject;
   regularInst: TMyType;
   copiedInst: TMyType;

{ TMyType }

begin
   regularInst := TMyType.Create(99);

   jsonObj := TJSON.ObjectToJsonObject(regularInst);
   asm nop end;
   WriteLn( jsonObj.ToJSON() ); // Ergibt {"someField":99,"fieldWithStupidName":99}

   copiedInst := TJson.JsonToObject<TMyType>(jsonObj);
   // copiedInst hat in someField Feld eine 42!

   readln;
end.
MyTypeUnit.pas
Delphi-Quellcode:
unit MyTypeUnit;

interface

type
   TMyType = class(TObject)
      protected var
         someField: Integer;
         FFieldWithStupidName: Integer;
      public
         constructor Create(); overload;
         constructor Create(const initValue: Integer); overload;
   end;

implementation uses System.SysUtils;

constructor TMyType.Create();
begin
   inherited Create();
   someField := 42;
   FFieldWithStupidName := 100;
end;

constructor TMyType.Create(const initValue: Integer);
begin
   inherited Create();
   someField := initValue;
   FFieldWithStupidName := initValue;
end;

end.

Das Problem:
Die Kopie bekommt das Feld "FFieldWithStupidName" richtig gesetzt, das Feld "someField" nicht.
Wie man in der Konsolenausgabe sieht kürzt der Kerl den "F"-Prefix bei Feldnamen weg. Beim Weg JSON->TObject wird er es wohl wieder davorsetzen.

Der Workaround:
Ich muss all meinen Feldern das Attribut JSonName mitgeben:
Delphi-Quellcode:
   TMyType = class(TObject)
      protected var
         [JSonName('someField')]
         someField: Integer;
         FFieldWithStupidName: Integer;
      public
         constructor Create(); overload;
         constructor Create(const initValue: Integer); overload;
   end;
Meine Frage:
Wie kann ich das vermeiden- Wo kann ich einstellen dass er bitte die Felder auch alle so serialisieren soll, wie sie tatsächlich heißen?
  Mit Zitat antworten Zitat
 

 

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 20:56 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz