neuecc/Utf8Json

How do you set Utf8Json to work with class objects with .ctors that have parameters

frankinstien opened this issue · 3 comments

I'm getting an unable to serialize a class object because its constructor has parameters. Is there a setting to bypass that and instantiate the object?

I placed a parameterless constructor in the class and now I'm getting:
Error message:
expected:'"', actual:'D', at offset:2

Stack trace:

at Utf8Json.JsonReader.ReadStringSegmentRaw() in I:\OneDrive\Utf8Json-master\src\Utf8Json\JsonReader.cs:line 766 at Utf8Json.JsonReader.ReadPropertyNameSegmentRaw() in I:\OneDrive\Utf8Json-master\src\Utf8Json\JsonReader.cs:line 796 at Utf8Json.Formatters.GloballySharedObjects_Models_CredentialsFormatter1.Deserialize(JsonReader& , IJsonFormatterResolver ) at Utf8Json.JsonSerializer.Deserialize[T](Byte[] bytes, Int32 offset, IJsonFormatterResolver resolver) in I:\OneDrive\Utf8Json-master\src\Utf8Json\JsonSerializer.cs:line 213 at Utf8Json.JsonSerializer.Deserialize[T](Byte[] bytes, IJsonFormatterResolver resolver) in I:\OneDrive\Utf8Json-master\src\Utf8Json\JsonSerializer.cs:line 199 at Utf8Json.JsonSerializer.Deserialize[T](String json, IJsonFormatterResolver resolver) in I:\OneDrive\Utf8Json-master\src\Utf8Json\JsonSerializer.cs:line 189 at Utf8Json.JsonSerializer.Deserialize[T](String json) in I:\OneDrive\Utf8Json-master\src\Utf8Json\JsonSerializer.cs:line 184 at BinarySerializer.FileSerializations.UTF8DeSerializer[T](String jSon) in I:\OneDrive\ThinDatabase\BinarySerializer\FileSerializations.cs:line 541
Here's the json:

{ID:9a42a291cf824fb5935ab4462755321c,UserName:ontologicalLogin,Password:123456,HashCode:0,AccessLevel:owner,Database:CategoriesDb,ConnectionId:NA,Date:3/1/2020}

@frankinstien That JSON doesn't seem like it's valid. I think you need quotes around pretty much everything in that snippet, except for the numerical values. (E.g., {"ID":"9a42...","UserName":"ontologica..."). I don't think it has anything to do with your constructors.

@frankinstien, @jwagon is correct, the JSON format provided is invalid. What did you use to generate your example string? Also, is this issue still relevant to keep open?

Going to assume your HashCode is an integer, and your Id was intended as a GUID/UUID.

Here's an example of a properly formatted JSON text:

{
  "ID": "9a42a291-cf82-4fb5-935a-b4462755321c",
  "UserName": "ontologicalLogin",
  "Password": "123456",
  "HashCode": 0,
  "AccessLevel": "owner",
  "Database": "CategoriesDb",
  "ConnectionId": "NA",
  "Date": "3/1/2020"
}

and the accompanying class,

public class YourClass
{
  public Guid ID { get; set; } 
  public string UserName { get; set; } 
  public string Password { get; set; } 
  public int HashCode { get; set; } 
  public string AccessLevel { get; set; } 
  public string Database { get; set; } 
  public string ConnectionId { get; set; } 
  public string Date { get; set; } 
}