Can XmlSerializer deserialize into a Nullable<int>? - xml-serialization

I wanted to deserialize an XML message containing an element that can be marked nil="true" into a class with a property of type int?. The only way I could get it to work was to write my own NullableInt type which implements IXmlSerializable. Is there a better way to do it?
I wrote up the full problem and the way I solved it on my blog.

I think you need to prefix the nil="true" with a namespace in order for XmlSerializer to deserialise to null.
MSDN on xsi:nil
<?xml version="1.0" encoding="UTF-8"?>
<entities xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="array">
<entity>
<id xsi:type="integer">1</id>
<name>Foo</name>
<parent-id xsi:type="integer" xsi:nil="true"/>

My fix is to pre-process the nodes, fixing any "nil" attributes:
public static void FixNilAttributeName(this XmlNode #this)
{
XmlAttribute nilAttribute = #this.Attributes["nil"];
if (nilAttribute == null)
{
return;
}
XmlAttribute newNil = #this.OwnerDocument.CreateAttribute("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance");
newNil.Value = nilAttribute.Value;
#this.Attributes.Remove(nilAttribute);
#this.Attributes.Append(newNil);
}
I couple this with a recursive search for child nodes, so that for any given XmlNode (or XmlDocument), I can issue a single call before deserialization. If you want to keep the original in-memory structure unmodified, work with a Clone() of the XmlNode.

The exceptionally lazy way to do it. It's fragile for a number of reasons but my XML is simple enough to warrant such a quick and dirty fix.
xmlStr = Regex.Replace(xmlStr, "nil=\"true\"", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"");

Related

Why does CanDeserialize always return false when deserialization succeeds?

I am attempting to deserialize an xml string into an object, nothing strange about that. Everything was fine until I upgraded my project to .Net5.
In the upgrade, I had to add a reference to the package Microsoft.XmlSerializer.Generator, and alter the project file to add the following:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.XmlSerializer.Generator" Version="1.0.0" />
</ItemGroup>
That allowed me to create the XmlSerializer (first error was just weird). Now, however, every call to CanDeserialize on the XmlReader return false if the class has the XmlRoot attribute. Now, I can deserialize the xml text. That does work. But why would CanDeserialize fail based on that condition?
Below is the class and the code I am using to test in a console app (.Net5).
[Serializable, XmlRoot("TestObj")]
//[Serializable]
public class TestObj
{
public int TestVal;
}
static void Main(string[] args)
{
var serializer = new XmlSerializer(typeof(TestObj));
//generated by doing a test serialization of the class
var teststr = "<TestObj xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><TestVal>2</TestVal></TestObj>";
using (var str = new StringReader(teststr))
using (var reader = XmlReader.Create(str))
{
if (serializer.CanDeserialize(reader))
Console.WriteLine(((TestObj)serializer.Deserialize(reader)).TestVal);
else
{
Console.WriteLine("Value cannot be deserialized into the given Type");
//try it anyway
var o = (TestObj)serializer.Deserialize(reader);
Console.WriteLine(o.TestVal);
}
}
}
My workaround is just to eliminate the CanDeserialize call and wrap the deserialization in a try.. catch, but I'm still curious why this is happening.
Okay, comments on the question from #dbc did lead me to try something else. A couple of details I left of the original question because it didn't seem relevant (due to the testing involved) is that the class I am trying to deserialize in my non-test is in a .Net Standard 2.1 library. While the project doing the deserialization is .Net5.
In the end, I got this work. I had to reference the Microsoft.XmlSerializer.Generator package in my .Net Standard based library, but not reference it in the .Net5 project.
In short, leaving the package reference off the Standard project caused one error, and including it in both caused another.

JMSSerializerBundle, MongoDB, and XML Metadata

I've been wracking my brain over this all night it seems. Every time I try to serialize the results from a document query, I receive a result of bool(false). Presumably this means that the serialization is failing (great! Perhaps you'd be willing to tell me why you're failing? No? didn't think so.)
I have told the JMSSerializerBundle where my FOSUserBundle mappings are held, e.g.:
jms_serializer:
metadata:
file_cache:
dir: "%kernel.cache_dir%/serializer"
auto_detection: true
directories:
FOSUserBundle:
namespace_prefix: "FOS\\UserBundle"
path: "%kernel.root_dir%/config/serializer/fosuser"
I've defined a very simple XML serializer metadata file, e.g. Model.User.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<serializer>
<class name="FOS\UserBundle\Model\User" xml-root-name="user" exclusion-policy="all">
<property name="username" type="string" expose="true" />
</class>
</serializer>
And, in a test controller I'm manually invoking the serializer before I attempt to do anything more sophisticated, e.g.:
class DefaultController extends Controller
{
public function serializeAction()
{
$userManager = // get the user manager
$users = $userManager->findUsers();
$serializer = $this->get('jms_serializer');
foreach ($users as $id => $user) {
Debug::dump('Processing user: '.$user->getUsername());
$result = $serializer->serialize($user, 'json');
Debug::dump($result);
}
/*return a response */
}
}
The result? Always bool(false).
However, if I manually construct a new User object, the serialization works fine, e.g.:
class DefaultController extends Controller
{
public function serializeAction()
{
$userManager = // get the user manager
$users = $userManager->findUsers();
$serializer = $this->get('jms_serializer');
$user = new User();
$user->setUsername('testusername');
Debug::dump('Processing user: '.$user->getUsername());
$result = $serializer->serialize($user, 'json');
Debug::dump($result);
/*return a response */
}
}
Any thoughts? I'm about ready to lose my mind over this.
EDIT: Also on further reflection, it is worth noting that it doesn't look like my serialization metadata is being respected. As shown above I'm only exposing the username, but when I create a new User in the controller and serialize it I see more than just that exposed, I'm seeing: username, enabled, locked, expired, roles, and credentials_expired. I suspect that I would be seeing every field, if it had non-null data in it.
EDIT (2): I've seen references to using the DoctrineObjectConstructor over the default object constructor for the serializer; and took the steps identified in this answer hoping that they might resolve my issue. Alas, they didn't (looks like that answer and question were more for deserialization though any way) and the result of a bool(false) persists.
EDIT (3): The issue wherein the serialization metadata was not being picked up, appears to be due to the mixing of auto_detection and the explicit declaration of directories, as illustrated in another question I've asked: JMSSerializerBundle mixing auto detection and explicit directories?

XSD2Code namespace issue

I am using XSD2Code to generate C# class from XSD file.
I got stuck with the following problem.
XML file looks something like
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Notification xmlns="http://message.domain.com">
<Object xmlns="http://type.domain.com" ID="97440" />
</Notification>
XML gets succefsully deserialized when xmls for Object is empty. But when there is a value like in the sample above, I get an error "Object reference not set to an instance of an object".
What could cause this error?
you have to change the Serializer to something like that
private static System.Xml.Serialization.XmlSerializer Serializer
{
get
{
if ((serializer == null))
{
serializer = new System.Xml.Serialization.XmlSerializer(typeof(Notification), "http://message.domain.com");
}
return serializer;
}
}
To turn off encoding, disable encoding on the Serialization tab

Drools Planner has trouble creating a configurer: how to debug

I tried to create a solver from XML configuration. But the entire process returns a cryptic error message that makes no sense.
How do I fix this? And how can I make sense of this to actually solve similar problems like this?
jesvin#Jesvin-Technovia:~/dev/drools/sudoku$ java App
Exception in thread "main" java.lang.NullPointerException
at org.drools.planner.core.domain.solution.SolutionDescriptor.processPropertyAnnotations(SolutionDescriptor.java:69)
at org.drools.planner.core.domain.solution.SolutionDescriptor.<init>(SolutionDescriptor.java:61)
at org.drools.planner.config.solver.SolverConfig.buildSolutionDescriptor(SolverConfig.java:197)
at org.drools.planner.config.solver.SolverConfig.buildSolver(SolverConfig.java:167)
at org.drools.planner.config.XmlSolverConfigurer.buildSolver(XmlSolverConfigurer.java:103)
at App.createSolver(App.java:62)
at App.main(App.java:40)
The function that throws it is listed here. The line is of course return configurer.buildSolver();.
private static Solver createSolver(){
XmlSolverConfigurer configurer = new XmlSolverConfigurer();
File file = new File("solver.xml");
FileInputStream fin = null;
try{
fin = new FileInputStream(file);
}
catch(IOException e){
System.out.println("Unable to read drl");
}
configurer.configure(fin);
//configurer.configure("/home/jesvin/dev/drools/sudoku/solver.xml");
return configurer.buildSolver();
}
The content of the XML:
<?xml version="1.0" encoding="UTF-8"?>
<solver>
<environmentMode>DEBUG</environmentMode>
<solutionClass>domain.Sudoku</solutionClass>
<planningEntityClass>domain.Digit</planningEntityClass>
<scoreDrl>score.drl</scoreDrl>
<scoreDefinition>
<scoreDefinitionType>SIMPLE</scoreDefinitionType>
</scoreDefinition>
<termination>
<scoreAttained>0</scoreAttained>
</termination>
<!--
<constructionHeuristic>
<constructionHeuristicType>FIRST_FIT_DECREASING</constructionHeuristicType>
<constructionHeuristicPickEarlyType>FIRST_LAST_STEP_SCORE_EQUAL_OR_IMPROVING</constructionHeuristicPickEarlyType>
</constructionHeuristic> -->
<constructionHeuristic>
<constructionHeuristicType>FIRST_FIT</constructionHeuristicType>
<moveFactoryClass>solution.RowChangeMoveFactory</moveFactoryClass>
</selector>
<acceptor>
<completeSolutionTabuSize>1000</completeSolutionTabuSize>
</acceptor>
<forager>
<!-- Real world problems require to use of <minimalAcceptedSelection> -->
</forager>
</localSearch>
</solver>
OP's addition:
The issue was related to an inadvertent write-only property. There was setBlockList and getBlocklist (small 'l' in the getter), which was a typo. Drools complained because it detected two properties, one of which was write-only.
The other mismatch was isFixed and setFixed. It works for boolean built-in, but not for Boolean object.
I solved the issue in a mailing list post.
Cryptic error message is fixed in 5.4.0.Beta1 (already released): https://issues.jboss.org/browse/JBRULES-3247

MEF Contrib Provider Model Not Importing Parts

I have been trying to use the configurable provider model for handling my MEF imports and exports from MEF Contrib (link). I've read the Codeplex documentation and Code Junkie's blog post (link); however, I can't seem to get the container to create the parts. Where am I going wrong?
Program.cs
namespace MEFTest
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Run();
}
// [ImportMany("command", typeof(IHelp))]
public IEnumerable<IHelp> Commands { get; set; }
void Run()
{
Compose();
foreach(IHelp cmd in Commands)
{
Console.WriteLine(cmd.HelpText);
}
Console.ReadKey();
}
void Compose()
{
var provider = new ConfigurableDefinitionProvider("mef.configuration");
var catalog = new DefinitionProviderPartCatalog<ConfigurableDefinitionProvider>(provider);
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
}
}
TestCommand.cs
namespace MEFTest
{
//[Export("command", typeof(IHelp))]
public class TestCommand : IHelp
{
private string _helpText = "This is a test.";
public string CommandName
{
get { return "Test"; }
}
public string HelpText
{
get { return _helpText; }
}
}
}
App.Config section:
<mef.configuration>
<parts>
<part type="MEFTest.TestCommand, MEFTest">
<exports>
<export contract="IHelp" />
</exports>
</part>
<part type="MEFTest.Program, MEFTest">
<imports>
<import member="Commands" contract="IHelp" />
</imports>
</part>
</parts>
</mef.configuration>
I don't get any build errors and it runs fine if I switch to the typical attribute-based system that is part of the MEF core (with the appropriate catalog too). Program.Commands is always NULL in the above example. I tried to just use a singular property instead of a collection and get the same results.
When I debug I can get the provider.Parts collection so I know it's accessing the configuration information correctly; however, I get an InvalidOperationException whenever I debug and try to drill into catalog.Parts.
Anyone have any experience as to where I'm going wrong here?
As documented here, you also need this in your config file:
<configSections>
<section
name="mef.configuration"
type="MefContrib.Models.Provider.Definitions.Configurable.PartCatalogConfigurationSection, MefContrib.Models.Provider" />
</configSections>
If you already have that, then it might be interesting to show us the stack trace of the InvalidOperationException that you get when accessing provider.Parts.
I had the same problems and could not get it to work, but here are some details:
It seems that ComposeParts() does not work as expected (at least in the version I used) because it uses static methods, based on Reflection to find all required Imports (so it seems that this part cannot be changed from outside of MEF). Unfortunately we want to use xml configuration and not the MEF attributes.
It works if you add [Import] attributes to the members of the class you you use with ComposeParts(). In your case this would be "Programm". In this case all exports defined in the configuration file will be found.
I could not find any documentation or examples on the MEF Contrib page relating to that problem. Also there is no unittest in the MEF contrib projekt that uses ComposeParts().
A workaround would be to use container.GetExportedValues() to retrieve the values, but in this case you have to set the classes members manually.
Hope that helps.