Read from File with multiple derived classes
See original GitHub issueHi,
I’m using your library primarily for supporting serialization and deserialization of polymorphic objects. I have a base class “DataSetBase” and two deriving classes, “DataSet” and “BlackoutDataSet”. Following the serialization:
JsonSerializerOptions options = new JsonSerializerOptions().SetupExtensions();
DiscriminatorConventionRegistry registry = options.GetDiscriminatorConventionRegistry();
registry.RegisterType<DataSet>();
registry.RegisterType<BlackoutDataSet>();
using (var fileWriter = new StreamWriter(_failureFolderPath + GetFailureFileName(), true)){
if (dataSet is DataSet)
{
jsonString = JsonSerializer.Serialize<DataSetBase>((DataSet)dataSet, options);
}
else if (dataSet is BlackoutDataSet)
{
jsonString = JsonSerializer.Serialize<DataSetBase>((BlackoutDataSet)dataSet, options);
}
else
{
throw Exception("Got unknown type");
}
if (jsonString != "")
fileWriter.WriteLine(jsonString);
}
How can I be sure of the type of the object when deserializing?
ReadOnlySpan<byte> jsonReadOnlySpan = File.ReadAllBytes(_failureFolderPath + GetFailureFileName());
var reader = new Utf8JsonReader(jsonReadOnlySpan);
while (reader.Read()){
var deserializedObject = JsonSerializer.Deserialize<????>(reader, options);
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Reading multiple derived classes from a file c++
My program consists at managing a stock of products of different types. I created 3 different classes for each product derived from a...
Read more >Multiple Inheritance in C++
Does C++ compiler create default constructor when we write our own? ... Why C++ is partially Object Oriented Language? ... C++ Function Overloading....
Read more >C++ fstream Writing & reading base and derived class ...
C++ fstream Writing & reading base and derived class objects. Copy #include <fstream> #include <iostream> #include <string> #include <vector> #include ...
Read more >Inheritance w/read() and write() - C++ Forum
I am writing and reading to/from a file. The parent class has a write/read function that covers the data that is relevant to...
Read more >Multiple Base Classes
A class can be derived from more than one base class. In a multiple-inheritance model (where classes are derived from more than one...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Don’t forget to setup a discriminator on each inherited class. You can decorate the class with JsonDiscriminatorAttribute or use ObjectMapping<T>.SetDiscriminator method.
Then you have to register your class in the discriminator registry.
For further details, see https://github.com/dahomey-technologies/Dahomey.Json/blob/master/README.md#polymorphism
Don’t hesitate to reopen if you have any problem.
StreamReader was the solution. Thanks!