grijjy / GrijjyFoundation

Foundation classes used by other Grijjy repositories
Other
247 stars 88 forks source link

Json de-serialization execute without exception for completely different two json strings #29

Closed ertankucukoglu closed 4 years ago

ertankucukoglu commented 4 years ago

Hello, I might have missed an option to set. If that is the case, just let me know what I should do, please.

Assume that you have following record definition:

  TSubReturn = record
  public
    code: Integer;
    desc: string;
  end;

  TReturn = record
  public
    code: Integer;
    desc: string;
    subReturn: TSubReturn;
  end;

  TEODBatchUploadResponse = record
  public
    &message: string;
    requiredFileSequence: Integer;
    ret: TReturn;
  end;

My regular json string is: {"EodBatchUploadResult":{"ResponseCode":"0","ResponseDescription":"","ResponseSubCode":null,"message":null,"requiredFileSequence":0,"ret":{"code":0,"desc":null,"subReturn":{"code":0,"desc":null}}}}

However, from time to time, server returns following response: {"ResultCode":2,"ResultMessage":"Error"}

Both of these example json strings are parsed as OK. Meaning, no exception during de-serialization operation raised.

I do not need to parse second example. On the other hand, I would like an exception or a reference like json is not parsed because none of the record fields exists in it.

Let me know if this is a feature, of Grijjy, please.

erikvanbilsen commented 4 years ago

Our library is designed to only deserialize fields that are present in the record definition. When a key is deserialized that is not in the record (like ResultCode), it is silently ignored.

This is by choice because it allows for forward and backward compatibility. For example, imagine the rest server is modified to return some additional information. If the deserialization engine would raise an exception when deserializing unknown fields, then the server change would break existing deserialization code.

Also, you may not be interested in everything that the rest server returns. Because we silently ignore unknown fields, you only have to declare those fields in your records that you care about.

In your example, you could add ResultCode and ResultMessage to your response record. If there are no errors, those fields will be ignored. But they will be set in case of an error.

Hope this makes sense...