2

I have serialized xpo objects into JSON data in separate project using default session. But when I try to de-serialize the same within view controller i am getting default session error. I am able to get the current session but I am not sure how to deserialize by using the current session.

Newtonsoft is being used to serialize and deserialize.

string filetext= File.ReadAllText("importedfile.json");
Plugin pl = (Plugin)JsonConvert.DeserializeObject(filetext,jsonsettings);

Plugin is my class name to which the JSON data should get deserialized.

dbc
  • 104,963
  • 20
  • 228
  • 340
Ashraf Ali
  • 31
  • 2
  • 1
    First of all you need to do `JsonConvert.DeserializeObject(filetext,jsonsettings)` to deserialize to a `Plugin`. See [Unable to cast object of type Newtonsoft.Json.Linq.JObject even though I am trying to cast to an object with matching properties](https://stackoverflow.com/a/48130933/3744182) for why. Does that solve your problem, or do you have some additional issue? If so might you [edit] your question to share a [mcve]? – dbc Jul 06 '21 at 13:55
  • Actually, `Plugin` is my XPO object that cannot be instantiated without the session. But I am not sure how to provide the session when deserializing. – Ashraf Ali Jul 07 '21 at 03:33
  • We need to see a [mcve] to answer your question, but maybe a [`CustomCreationConverter`](https://www.newtonsoft.com/json/help/html/CustomCreationConverter.htm) that creates the `Plugin` from some factory method would work. – dbc Jul 07 '21 at 03:43
  • Is this how your `Plugin` is created: https://docs.devexpress.com/XPO/2077/create-a-data-model/create-a-persistent-object#declare-a-persistent-object – dbc Jul 07 '21 at 14:25

1 Answers1

1

Presuming your Plugin type inherits from DevExpress.Xpo.XPObject in the manner shown in the documentation example:

public class Plugin : XPObject {
    // XPObject has a built-in Integer key and you can't add a custom key
    public Plugin(Session session) : base(session) { }
    string fMyProperty;
    public string MyProperty {
        get { return fMyProperty; }
        set { SetPropertyValue(nameof(fMyProperty), ref fMyProperty, value); }
    }
}

And assuming that your deserialization code already has access to some Session session, then define the following CustomCreationConverter<Plugin>:

public class PluginConverter : CustomCreationConverter<Plugin>
{
    DevExpress.Xpo.Session Session { get; }

    public PluginConverter(DevExpress.Xpo.Session session) => this.Session = session ?? throw new ArgumentNullException();

    public override Plugin Create(Type objectType) => new Plugin(Session);
}

And deserialize as follows:

Session session; // Your session

var jsonsettings = new JsonSerializerSettings
{
    Converters = { new PluginConverter(session), /* Other converters as required */ },
    // Other settings as required
};
var pl = JsonConvert.DeserializeObject<Plugin>(filetext, jsonsettings);

When deserializing the type Plugin the converter's Create() method will be invoked to manufacture the Plugin using the provided session.

If you are using multiple sessions, you will need to use a new JsonSerializerSettings for each new session.

This pattern can be used for any type inheriting from XPObject with a public constructor taking a Session object.

dbc
  • 104,963
  • 20
  • 228
  • 340