Assume that i have these classes:
public class BaseEntity
{
public int id;
}
public class A : BaseEntity
{
public int aField;
public C c;
}
public class B : BaseEntity
{
public string bField;
public C c;
}
public class C : BaseEntity
{
public string cField;
}
Now, i want to use JSON.Net JsonConverter
annotation to serialize C
as below when i want to load A
or B
objects. for example something like this:
[JsonConverter(typeof(JsonConverterCustomImpl))]
public class C
{
public string cField;
}
and the result of serialized A
or B
objects should be like this:
// A object
{
id: 0,
aField: 0,
cField: ''
}
// B object
{
id: 0,
bField: 0,
cField: ''
}
I dont know how shoud=ld i implement the JsonConverterCustomImpl
class.
UPDATE
i'm used from this answer, also defined FlattenJsonConverter
class and set it to JsonAnnotation of A
and B
, but when i run the project, thi exception was thrown:
An unhandled exception of type 'System.StackOverflowException' occurred in Newtonsoft.Json.dll
UPDATE
See this below diagram, The C
class is Attachment
in my application and many of models may be contains that.
The Attachment
class have a byte[] fileContent
field that contains the uploaded file content. So i want to serialize this class as flatten with container class to have a easily access to fileContent
in UI
side.
I found this way to Serialize C
class flatten, but it throws exception when using from JsonConverter
in annotation.
NOTE
I'm serialize fileContent
as a base64 string
.