question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Read from File with multiple derived classes

See original GitHub issue

Hi,

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:closed
  • Created 4 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mcatanzariticommented, Jan 20, 2020

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.

0reactions
DeckardCain2014commented, Jan 27, 2020

StreamReader was the solution. Thanks!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found