0

I'm trying to deserialize a ParseObject (as described in the parse platform developer's guide). I'm getting an error that JSON cannot deserialize that object.

So I have created this:

public class Phase
{
    public string Details { get; set; }
    public string ObjectId { get; set; }
    public int PhaseId { get; set; }
    public bool Completed { get; set; }
    public string PhaseListName { get; set; }
    public int Position { get; set; }
    public int ButtonId { get; set; }
}

private List<ParseObject> ConvertToParseObjectList(List<Phase> phaseObjects)
{
    var _objects = new List<ParseObject>();
    for (int i = 0; i < phaseObjects.Count; i++)
    {
        var _obj = new ParseObject("CasePhases");
        _obj.ObjectId = phaseObjects[i].ObjectId;
        _obj["Details"] = phaseObjects[i].Details;
        _obj["PhaseId"] = phaseObjects[i].PhaseId;
        _obj["Completed"] = phaseObjects[i].Completed;
        _objects.Add(_obj);
    }
    return _objects;
}

private List<Phase> ConvertToPhaseObjectList(List<ParseObject> parseObjects)
{
    var target = parseObjects.ConvertAll(x => new Phase
    {
        Details = x.Get<string>("Details"),
        ObjectId = x.ObjectId,
        PhaseId = x.Get<int>("PhaseId"),
        Completed = x.Get<bool>("Completed")
    });
    return target;
}

So I can serialize and deserialize like this:

ConvertToParseObjectList(JsonConvert.DeserializeObject<List<Phase>>(Settings.PhaseObjects));
JsonConvert.SerializeObject(ConvertToPhaseObjectList( application.casePhases));

It works, but I'm trying to find out how I can tell JSON how to deserialize the object using the JsonObject and JsonProperty or something like this.

The line that thows an exception is this:

JsonConvert.DeserializeObject<List<ParseObject>>(Settings.PhaseObjects);

And the Trace is:

Xamarin caused by: android.runtime.JavaProxyThrowable: Newtonsoft.Json.JsonSerializationException: Cannot create and populate list type Parse.ParseObject. Path '[0]', line 1, position 2.
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewList(JsonReader reader, JsonArrayContract contract, ref bool createdFromNonDefaultCreator)<2781d1b198634655944cdefb18b3309b>:0
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, object existingValue, string id)<2781d1b198634655944cdefb18b3309b>:0
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, object existingValue)<2781d1b198634655944cdefb18b3309b>:0
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, string id)<2781d1b198634655944cdefb18b3309b>:0
md5b8608fa4770a86d5bda0b33ebcfbec27.LiveSurgery.n_onResume(Native Method)
md5b8608fa4770a86d5bda0b33ebcfbec27.LiveSurgery.onResume()LiveSurgery.java:65
android.app.Instrumentation.callActivityOnResume()Instrumentation.java:1197
android.app.Activity.performResume()Activity.java:5496
android.app.ActivityThread.performResumeActivity()ActivityThread.java:2975
android.app.ActivityThread.handleResumeActivity()ActivityThread.java:3018
android.app.ActivityThread.handleLaunchActivity()ActivityThread.java:2422
android.app.ActivityThread.access$800()ActivityThread.java:151
android.app.ActivityThread$H.handleMessage()ActivityThread.java:1342
android.os.Handler.dispatchMessage()Handler.java:110
android.os.Looper.loop()Looper.java:193
android.app.ActivityThread.main()ActivityThread.java:5322
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke()Method.java:515
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run()ZygoteInit.java:829
com.android.internal.os.ZygoteInit.main()ZygoteInit.java:645
dalvik.system.NativeStart.main(Native Method)
CDrosos
  • 2,418
  • 4
  • 26
  • 49
  • What is the definition of `ParseObject`? – Brian Rogers May 20 '17 at 15:47
  • @BrianRogers there are objects from the parse.net platform http://docs.parseplatform.org/dotnet/guide/ is this what you ask? – CDrosos May 20 '17 at 15:49
  • Yes, that is what I was asking. I didn't know whether this was a third-party class or one you had created. – Brian Rogers May 20 '17 at 15:54
  • @dbc i have update my Question, it appears someone has do something like this here http://stackoverflow.com/questions/39866578/how-to-save-parseobject-to-disk-in-net but i cant understand how – CDrosos May 20 '17 at 23:50
  • 1
    @CDrosos - OK, looking at your traceback and at [the source](https://github.com/parse-community/Parse-SDK-dotNET/blob/master/ParseCore/Public/ParseObject.cs) the problems are that 1) `ParseObject` implements `IEnumerable>` so is serialized as an array but has no public parameterless constructor or constructor taking an `IEnumerable>` argument so it cannot be deserialized. 2) While this is fixable (though not easily; see [here](http://stackoverflow.com/q/26418923/3744182) the values are *untyped* so type identity will be lost. – dbc May 21 '17 at 01:19
  • 1
    So, rather than trying to store a `ParseObject` why not just store your `Phase` POCO? Json.NET should be able to (de)serialize it without difficulty and you already have code to map it to a `ParseObject`. – dbc May 21 '17 at 01:21
  • 1
    @dbc I was thinking if my solution wasnt the best, but as i see it might is so i will leave it as it is, thanks :D Also, what is POCO? :D – CDrosos May 21 '17 at 01:23
  • 1
    [Plain Old c# Object](https://stackoverflow.com/q/250001/3744182). Even Microsoft [uses the term](https://msdn.microsoft.com/en-us/library/dd456853(v=vs.100).aspx). – dbc May 21 '17 at 01:26
  • 1
    @dbc Hahaha, it sounds so funny to me :D. grab a POCO ASAP :D – CDrosos May 21 '17 at 01:27

0 Answers0