Bug 24708 - datacontractjsonserializer not calling subclass - repro available
Summary: datacontractjsonserializer not calling subclass - repro available
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Runtime.Serialization ()
Version: 3.8.0
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-11-20 15:31 UTC by Chris
Modified: 2018-03-13 12:07 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Repo project (9.91 KB, application/zip)
2014-11-24 17:07 UTC, Kent Green [MSFT]

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Chris 2014-11-20 15:31:00 UTC
See this for SO


This is a program that is ready to run in Xamarin and VS2013.   

I'm having an issue where mono isn't calling the serializer Subclass<T>, and need to work around that issue.

**Bug / workaround needed**

How should I modify SetMembershipProof so that it will call a method with the attribute [OnSerializing] located in a nested subclass of <T>?

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Json;
    using System.Text;
    using System.Threading.Tasks;
    namespace MonoBug
        class Program
            static void Main(string[] args)
                SetMembershipProof2 setMembershipProof = new SetMembershipProof2();
                string setProofJSON =    CryptoSerializer.Serialize<SetMembershipProof2>(setMembershipProof);
               // Inspect the contents of SetProofJSON, it is null under mono, and not null in .NET
        public class CryptoSerializer
            /// <summary>
            /// Serialize serializable types in namespace UProveCrypto.PolyProof.
            /// </summary>
            /// <typeparam name="T">input type</typeparam>
            /// <param name="obj">instance of serializable type</param>
            /// <returns>JSON string</returns>
            public static string Serialize<T>(T obj)
                string result;
                    using (MemoryStream ms = new MemoryStream())
                        DataContractJsonSerializer jsonSerializer =
                            new DataContractJsonSerializer(obj.GetType());
                        jsonSerializer.WriteObject(ms, obj);
                        ms.Position = 0;
                        StreamReader reader = new StreamReader(ms);
                        result = reader.ReadToEnd();
                catch (Exception e)
                    throw new SerializationException(obj.GetType().Name, e);
                return result;
        public abstract class GroupParameterizedSerializer2
            public void SerializeGroup(StreamingContext context)
        public class SetMembershipProof2 : GroupParameterizedSerializer2
            #region Serialization
            /// <summary>
            /// Serialization of a
            /// </summary>
            [DataMember(Name = "a", EmitDefaultValue = false, Order = 2)]
            internal string[] _a;
            /// <summary>
            /// Serialization of c
            /// </summary>
            [DataMember(Name = "c", EmitDefaultValue = false, Order = 3)]
            internal string[] _c;
            /// <summary>
            /// Serialization of r
            /// </summary>
            [DataMember(Name = "r", EmitDefaultValue = false, Order = 4)]
            internal string[] _r;
            /// <summary>
            /// Serialize a, c, r.
            /// </summary>
            /// <param name="context">The streaming context.</param>
            internal void OnSerializing(StreamingContext context)
                Console.WriteLine("Debug: This isn't called in Mono...");
                List<string> t = new List<string>();
                _a = t.ToArray();
                _c = t.ToArray();
                _r = t.ToArray();
Comment 1 Kent Green [MSFT] 2014-11-24 17:07:01 UTC
Created attachment 8853 [details]
Repo project

Customer from this desk case:

Reported they were hitting this issue. (Stated directly, w/ link to this bug report.) And asked for info about it's status. Since it looked like the report didn't have much info, I requested their version info, log files & sample project. 

This attachment is the sample project, I was able to build it successfully; but other than that I'm not sure what I'm looking for.

They haven't sent over the logs yet so I'm asking for those again; and will add them when they are received. 

---Customer's build info---
=== Xamarin Studio ===

Version 5.5.4 (build 15)
Installation UUID: 748b35df-dbee-4077-a803-dd0e95b67eb7
Mono 3.10.0 ((detached/92c4884)
GTK+ 2.24.23 (Raleigh theme)

Package version: 310000031

=== Xamarin.Android ===

Version: (Trial Edition)
Android SDK: /Users/Chris/Library/Developer/Xamarin/android-sdk-macosx
Supported Android versions:
2.1 (API level 7)
2.2 (API level 8)
2.3 (API level 10)
3.1 (API level 12)
4.0.3 (API level 15)
4.4 (API level 19)
5.0 (API level 21)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)

=== Apple Developer Tools ===

Xcode 6.1 (6604)
Build 6A1052d

=== Xamarin.Mac ===


=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 840a925
Build date: 2014-11-16 21:03:22-0500

=== Build Information ===

Release ID: 505040015
Git revision: f93940a35458a18052f1a25e106e62ca970d9c40
Build date: 2014-11-19 15:32:41-05
Xamarin addins: dc23cbd91a3a0e1d326328e1229e86c942a49ec8

=== Operating System ===

Mac OS X 10.10.1
Darwin Chriss-MacBook-Pro.local 14.0.0 Darwin Kernel Version 14.0.0
Fri Sep 19 00:26:44 PDT 2014
root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64
Comment 2 Chris 2014-11-24 18:08:18 UTC
To be clear the issue I'm having is that .NET will call the  [OnSerializing] method, while mono does not.

This means the resulting output for  _a, _c, and _r is null

    DataContractJsonSerializer jsonSerializer =
                            new DataContractJsonSerializer(obj.GetType());

In my mind this is a defect, since .NET can serialize the type properly, and the values shouldn't be null.

I will add the logs shortly as an attachment.
Comment 4 Marek Safar 2018-03-13 12:07:44 UTC
Already fixed in recent Mono versions