0

I have an object with a property of type as "object" (need this for have a generic code).

Public Property Valeur() As Object

I serialize it with DataContractJsonSerializer , and i have this:

            }, {
            "Desc": "Date ouverture",
            "Id": "DatOuverture",
            "Mode": 0,
            "Valeur": "\/Date(1482966000000+0100)\/"
        }, {

So, when i deserialize it, the serializer cast "Valeur" in string, not in dateTime.

Valeur = "\/Date(1482966000000+0100)\/"

I tried with "AlwaysTransmitTypeInformation", but that change nothing. When i write in the property before serialise, she's well in "DateTime" type, not string.

For information, Valeur can be a different type that dateTime.

Anybody have an idea ?

Regard.

david
  • 170
  • 10
  • seems serialization cannot cast dynamic types automatically. – Sebastian L Jan 16 '17 at 08:46
  • Time looks like it may be a long : DateTime time = DateTime.FromBinary(1482966000000); – jdweng Jan 16 '17 at 08:52
  • These are just dates that are problematic, other types (even complex objects) work well. – david Jan 16 '17 at 08:54
  • Hello Jdweng, My property can also be a numeric or boolean, ..., i need a format date for identifiate the type. – david Jan 16 '17 at 09:57
  • There's one solution to a similar question given here: [How to serialize/deserialize a DateTime stored inside an object field using DataContractJsonSerializer?](http://stackoverflow.com/a/40004840/3744182). Does that meet your needs? – dbc Jan 17 '17 at 10:05

1 Answers1

0

You could specify the format for dates using DataContractJsonSerializerSettings when constructing your serializer object:

public class Item
{
    public object Value { get; set; }
}


class Program
{
    static void Main()
    {
        var settings = new DataContractJsonSerializerSettings
        {
            DateTimeFormat = new DateTimeFormat("o"),
        };
        var serializer = new DataContractJsonSerializer(typeof(Item), settings);

        var item = new Item { Value = DateTime.UtcNow };
        serializer.WriteObject(Console.OpenStandardOutput(), item);
    }
}

will serialize as:

{"Value":"2017-01-16T08:50:47.9127678Z"}

As an alternative you could use Json.NET which gives you far more control over the serialization process.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Hello Darin, I use another constructor of DataContractJsonSerializer : DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T), null, int.MaxValue, false, dcs, AlwayEmitTypeInformation); ... So, the DataContractJsonSerializerSettings parameter is not accessible. I look for set it before the instanciation, like a property set, but i can't find the property named "..Setting". – david Jan 16 '17 at 09:52
  • Which version of the .NET framework you are using? This settings is available from .NET 4.5. If you are using an older version of the framework you are out of luck. I would recommend you switching to Json.NET. – Darin Dimitrov Jan 16 '17 at 09:58
  • I'm on FrameWork 4.0. Also, i tried to using Json.Net, but i meet another problem, all objects are JObject type Maybe i'm not use it correctly: `Dim js As Newtonsoft.Json.Linq.JArray = Newtonsoft.Json.Linq.JArray.Parse(json) Dim ret As T = js.ToObject(Of T)() Return ret` – david Jan 16 '17 at 10:10
  • You need to define a strongly typed model and then deserialize to this model instead of calling `JArray.Parse`. – Darin Dimitrov Jan 16 '17 at 10:13
  • I'm not sur to understand... T is a strongly typed model. Could you give me a example please ? – david Jan 16 '17 at 10:41
  • So you are trying to deserialize something back to an `object` type on your model? How do you expect this to work? The serializer cannot possibly know what type to instantiate, unless of course you write a custom JsonConverter. There should be type information included in the JSON if you want this to work out of the box. But that's not recommended . I would avoid using `object` types but rather strongly typed view models. – Darin Dimitrov Jan 16 '17 at 10:42
  • I have made a IDataContractSurrogate with this in GetDeserializedObject: `If (CType(obj, tblFiltreChamp).Valeur.ToString().StartsWith("/Date(")) Then Dim strTmp As String = CType(obj, tblFiltreChamp).Valeur.ToString().Substring(6) Dim tmp As Long = 0 If (Long.TryParse(strTmp.Substring(0, strTmp.Length - 7), tmp)) Then CType(obj, tblFiltreChamp).Valeur = New DateTime(tmp) End If End If` That's working, but the date is not good date... While tmp is the good ticks in DAte([ticks]-1000)... 0_0 – david Jan 16 '17 at 11:38
  • Good: [link]http://stackoverflow.com/questions/12482856/proper-way-to-convert-json-date-to-net-datetime-during-deserialization – david Jan 16 '17 at 13:29