7 Replies - 149 Views - Last Post: 10 July 2019 - 01:17 PM Rate Topic: -----

#1 DrDress   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 23-July 12

Saving object with JavascriptSerializer

Posted 10 July 2019 - 08:03 AM

Hi I'm trying this thing that I saw online, where you can turn any object into a string and back again. The upshot is that one can save this string and hence the object to a file for later use. But I get an error beyond my understanding. This is my code:

Annealing Annealer = new Annealing() //This is my own weird class with various variables and methods

string SaveStr = new JavascriptSerializer().Serialize(Annealer);
Annealing LoadAnnealer = new JavascriptSerializer().Deserialize<Annealing>(SaveStr);



But I get this error on the last line:

ArgumentException unhandled
An unhandled exception of type 'System.ArgumentException' occurred in System.Web.Extensions.dll
Additional information: Object of type 'System.Drawing.Color[]' cannot be converted to type 'System.Drawing.Color[,]'.



It looks like is has written a jagged 2D array as a normal 1D array. I do have this array in my object. But I have no idea why or how this happened. The text is like 60 kb so it would take for ever to find out where this happened and still I would know how to fix it. Is there somethin I need to do when creating arrays in the object or what?

This post has been edited by modi123_1: 10 July 2019 - 09:35 AM
Reason for edit:: Fixed the title


Is This A Good Question/Topic? 0
  • +

Replies To: Saving object with JavascriptSerializer

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15113
  • View blog
  • Posts: 60,464
  • Joined: 12-June 08

Re: Saving object with JavascriptSerializer

Posted 10 July 2019 - 08:04 AM

Is this C#, Java, or Javascript?
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: Saving object with JavascriptSerializer

Posted 10 July 2019 - 08:57 AM

DrDress: Java != Javascript

Anyway, you should read the JavascriptSerializer documentation:

Quote

Json.NET should be used for serialization and deserialization.


View PostDrDress, on 10 July 2019 - 11:03 AM, said:

But I have no idea why or how this happened. The text is like 60 kb so it would take for ever to find out where this happened and still I would know how to fix it.

Why not serialize a smaller object graph? You don't have to test with a full sized object while you are trying to diagnose a problem.

Anyway, with most serializers/deserializers there is a way to tag your fields/properties so that you can coerce how they are written out or read in. As noted above, use Json.NET. There is more documentation and collective knowledge that you can tap into to do that kind of customization.
Was This Post Helpful? 0
  • +
  • -

#4 DrDress   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 23-July 12

Re: Saving object with JavascriptSerializer

Posted 10 July 2019 - 09:32 AM

Thanks for the reply. I was hoping not to have to read and understand how this whole thing works. All I want to do is to save an object to a file and retrieve it later on. I don't care about Java or Javascript or the serializer in generel (I only just learned that word :bigsmile: ). I could of cause write my storage-to-file function but this would take time and needs constant updating.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: Saving object with JavascriptSerializer

Posted 10 July 2019 - 09:38 AM

If you don't care particularly about the format as long as it is serialized, consider the XmlSerializer.
Was This Post Helpful? 0
  • +
  • -

#6 DrDress   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 23-July 12

Re: Saving object with JavascriptSerializer

Posted 10 July 2019 - 10:59 AM

Ok I tried that:

            Annealing Annealer = new Annealing(r, 50, listBox1);


            FileStream Stream = new FileStream("Save.dat", FileMode.Create);
            XmlSerializer XML = new XmlSerializer(typeof(Annealing));
            XML.Serialize(Stream, Annealer);



This time I get this error:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll

Additional information: There was an error reflecting type 'RandomCircles.Annealing'.



I don't know what a reflectimg type is :dozingoff:
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: Saving object with JavascriptSerializer

Posted 10 July 2019 - 12:24 PM

It is trying to perform reflection on the type RandomCircles.Annealing. Reflection is the runtime process of trying to determine names and parts of a class. Think of looking at your reflection in a mirror to see what you look like. The running code is trying to see what it looks like.

In Visual Studio, click on the "Exception Details" and keep digging down as far as you can through the InnerExceptions until you get to the bottom most type. Chances are that type is not serializable to XML. If you don't need that thing to be serialized, you can mark it as such using the XmlIgnoreAttribute.

Either you can try using JSON.NET to serialize to JSON, or have to narrow down what part of your class hierarchy is causing the JavascriptSerializer to choke when it's time to deserialize.
Was This Post Helpful? 0
  • +
  • -

#8 DrDress   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 23-July 12

Re: Saving object with JavascriptSerializer

Posted 10 July 2019 - 01:17 PM

Ok. Thanks a lot!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1