Bug 24708 - datacontractjsonserializer not calling subclass - repro available
Summary: datacontractjsonserializer not calling subclass - repro available
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Runtime.Serialization (show other bugs)
Version: 3.8.0
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-11-20 15:31 UTC by Chris
Modified: 2017-09-01 11:36 UTC (History)
3 users (show)

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


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

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

http://stackoverflow.com/questions/26953606/how-do-i-work-around-datacontractjsonserializer-not-calling-subclasst-method-w


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;
    
                try
                {
                    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;
            }
    
    
          
        }
        [DataContract]
        public abstract class GroupParameterizedSerializer2
        {
             
            [OnSerializing]
            public void SerializeGroup(StreamingContext context)
            {
    
     
            }
    
        }
        [DataContract]
        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>
            [OnSerializing]
            internal void OnSerializing(StreamingContext context)
            {
                Console.WriteLine("Debug: This isn't called in Mono...");
     
                List<string> t = new List<string>();
                t.Add("data1");
                _a = t.ToArray();
    
                t.Clear();
                t.Add("data2");
                _c = t.ToArray();
    
                t.Clear();
                t.Add("data3");
                _r = t.ToArray();
            }
     
            #endregion
        }
    }
Comment 1 Kent Green [MSFT] 2014-11-24 17:07:01 UTC
Created attachment 8853 [details]
Repo project

Customer from this desk case:
https://xamarin.desk.com/agent/case/106164

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
Runtime:
Mono 3.10.0 ((detached/92c4884)
GTK+ 2.24.23 (Raleigh theme)

Package version: 310000031

=== Xamarin.Android ===

Version: 4.20.0.28 (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 ===

Version:

=== Xamarin.iOS ===

Version: 8.4.0.43 (Business Edition)
Hash: 840a925
Branch:
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.

Note You need to log in before you can comment on or make changes to this bug.