Comments (6)
Why are you using typeof(object)
and not typeof(Speicherstand)
? Ok, saw your edit now. You need to identify the type and send it over the network, somehow. Most HTTP frameworks do this through a custom header (like RavenDB's Raven-Clr-Type
) or through a custom Content-Type
(like Content-Type: vnd.company-x/speicherstand+xml
). If you don't tell Yax what type it's supposed to deserialize, how is it going to know?
from yaxlib.
Hello, thanks for the comment.
I cant use typeof(Speicherstand)
, because the api i wrote does not know what the object type is.
I wrote a method which basically receives a message containing a xml serialized with yaxlib. then the method deserializes the xml to a object. after that in the caller method i could simply cast it to the given object it needs to be.
The problem is here, that the serialization comes from over the network so basically i dont know which object it is. e.g. "Speicherstand" or "quit" or something like that.
from yaxlib.
Yes, but then how should the deserializer interpret the meta of object type? Object is not enough..., at least an interface would be nice....
On Friday, November 7, 2014 4:40 PM, Michael Weinberger [email protected] wrote:
Hello, thanks for the comment.
I cant use typeof(Speicherstand), because the api i wrote does not know what the object type is.
I wrote a method which basically receives a message containing a xml serialized with yaxlib. then the method deserializes the xml to a object. after that in the caller method i could simply cast it to the given object it needs to be.
—
Reply to this email directly or view it on GitHub.
from yaxlib.
Object is not enough.
Why does the serializer then writes in the first tag: yaxlib:realtype=\"WpfApplication2.Speicherstand\"
?
Should not it detect it then afterwards again and create one via the properties?
from yaxlib.
I think is a feature request.
from yaxlib.
Expressing this issue in code and conclude:
XML can only be deserialized to a specific class, when
- the serializer instance is created for this class, OR
- the serializer is created for
typeof(object)
, but the XML contains theyaxlib:realtype
attribute.
namespace YAXLibTests;
public class Issue31SampleClass
{
public string Username { get; set; }
public int Granny { get; set; }
public int Factory { get; set; }
public int Farm { get; set; }
public int Mine { get; set; }
public int Cursor { get; set; }
}
[TestFixture]
public class Issue_31
{
[Test]
public void DeSerialize()
{
var original = new Issue31SampleClass { Cursor = 1, Factory = 2, Farm = 3, Granny = 4, Mine = 5 };
var s = new YAXSerializer(typeof(object));
var xml = s.Serialize(original);
/*
<Object yaxlib:realtype="YAXLibTests.Issue31SampleClass" xmlns:yaxlib="http://www.sinairv.com/yaxlib/">
<Username />
<Granny>4</Granny>
<Factory>2</Factory>
<Farm>3</Farm>
<Mine>5</Mine>
<Cursor>1</Cursor>
</Object>
*/
var deserialized = (Issue31SampleClass) s.Deserialize(xml);
deserialized.Should().BeEquivalentTo(original);
var xmlWithOutRealType =
xml.Replace(
"<Object yaxlib:realtype=\"YAXLibTests.Issue31SampleClass\" xmlns:yaxlib=\"http://www.sinairv.com/yaxlib/\">",
"<Object>");
// Create a new serializer instance!
s = new YAXSerializer(typeof(object));
Assert.That(() =>
{
// The serializer cannot guess which type to create
_ = (Issue31SampleClass) s.Deserialize(xmlWithOutRealType);
}, Throws.TypeOf(typeof(InvalidCastException)));
}
}
from yaxlib.
Related Issues (20)
- Dictionary serialization incorrect when publishing to single file HOT 4
- Generic deserialization methods should not hide base methods with 'new', e.g. with a generic base class HOT 1
- Possible regression caused by PR #183 HOT 9
- Extend tests to cover YAXSerializer and YAXSerializer<T> HOT 2
- Extend caching and pooling for better performance HOT 10
- Split IYAXSerializer<T> to IYAXSerializer<T> and IYAXDeserializer<T> HOT 7
- Make Exception a KnownType HOT 4
- All public members of YAXSerializer (generic/non-generic) should be fully covered by unit tests HOT 1
- How serialize a list as an attribute HOT 7
- Extend SerializationContext for pre-processing in ICustomSerializer and IKnownType
- Add unit tests for YAXCollectionItemTypeAttribute and YAXElementForAttribute
- Add YAXSerializationOptions.StripInvalidXmlChars HOT 3
- Structs are not serialized correctly HOT 3
- Update docs for new v4 features
- Multiple Elements with Same Name? How to Deserialize?
- Add support for DateOnly and TimeOnly types
- Is it possible deserialize to property by attribute name and inner xml with YAXLib?
- Serializing/Deserializing ValueTuples HOT 1
- Serialize not filtering properties correctly in .NET Standard HOT 6
- Serialized strings that use the stringbuilder pool throw when the string's length is > 1024 (DefaultStringBuilderCapacity).
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yaxlib.