Does the .NET binary serializer encode assemby information? - c#

I've been developing a game and part of the process was going to be a custom level tool and file format. I had created a small console application that generated a 'LevelAsset' object which contained only primative data types. The problem arises when I try to deserialize that data within my unity game. I didn't copy any code from my console app and rewrote both the 'LevelAsset' object and Deserialization code from scratch. Yet when I run the game, I get an error saying I'm missing the "Level Tool, version=1.0.0.0" assembly, which is from my original console app. How is this occuring. Is the binary serializer encoding information about the project?
Edit: So upon further investigation it appears that the BinnaryFormatter does indeed include information about the project in the form of a file header. So my new question is now: How on earth do I serialize without headers?

So after a bit of digging its fairly obvious whats happening. BinaryFormatter does indeed encode project information in its file headers. If you want to avoid this you should use binary writer instead.

Related

What is the easiest way to generate an App.Config file through pure c# code

I know the easiest way to generate a config file is through visual studio. however the environment my program is going to be functioning in we are going to have several different configurations and the application needs to be able to build the config files on its own. Just curious if there is an easier way than making a large string literal and then copying over to a new file. Thanks for any help.
Not sure what kind of information you want to save in generated configurations.
If you are using only appSettings section which as only key values, then it would be better to generate a JSON file. It is very easy to generate it using newtonsoft.json.
in your app.config file you can keep the path of JSON file and load the settings at app startup if the file is already available.
NOTE:
JSON can also store any kind of complex configurations, you will have to generate the classes to hold those configurations.
Once you application puts value in these objects, serialize it to JSON and keep it in appropriate folder which is accessible to application.
Hope this helps.

write / add / rewrite a class at runtime c# unity

in my project i have some sort of level builder that create a new text file and save all my wanted data as readable json string in 1 line.
while the project run in web build or in unity it self i can read the levels from that text file and every thing working great, in mobile builds that doesn't work.
my question is:
is there a way to create or add lines to a class at runtime?
for example write a new string in the class at run time that will stay there after run time is over?
No, there isn't. At least not at mobile platforms.
But you should be able to parse JSON on mobile platforms if you set the Api Compatibility Level to .Net 2.0, not .Net 2.0 Subset and disable Strip Engine Code in the Player Setting.
As #Tijmen said there is no way to change a C# class at runtime. But I see no reason to do so. Instead you should change the JSON string, write it to the file and recreate the level instance.
Looking at your code reveals that you are writing to Application.dataPath which is not writable in iOS player. So it should work when your are using Application.persistentDataPath.
Further on I would refrain from calling the folder Resources as this has a special meaning in Unity.
No there isn't, but you can make an ArrayList instead and put the info from the text file into the ArrayList. Then extract information from the ArrayList.

Configuring PCL project with a JSON file

I'm currently designing a cross-plateform application in Visual Studio 2013 using a Portable Class Library.
At startup, I want to have a certain object, created by deserializing a JSON file. This object will be used to configure certain resources in my project (e.g. certain button names, etc.).
I know how to parse the JSON file using Json.NET and to create the wanted object.
What I don't know is how to use it, and especially, where must it be generated? I don't think this JSON reading part could (and should) be added to the PCL, for the file reading part at least.
Thanks in advance (I hope this isn't a duplicate!)

C# JSON decoding using Web Matrix helpers

I've been searching for a while for a nice and clean way to convert a JSON object to a dynamic object.
(I could cast to an object but the Twitter Streaming API actually sends two different JSON objects with the possibility of future object types!)
The code I use currently is from:
Deserialize JSON into C# dynamic object?
But its not the cleanest code and I was playing around with Web Matrix and noticed that they have a nice JSON.Decode(string) and JSON.Encode(object) methods and wanted to make use of them.
http://msdn.microsoft.com/en-us/library/system.web.helpers.json(v=vs.99).aspx
Adding a reference to System.Web.Helpers to my C# console application I managed to compile a solution calling JSON.Decode but... it throws a nasty exception.
This is probably down to me using it in a way not intended (outside Web Matrix) but any ideas? Probably expecting a simple, no thats silly answer ;-)
Attempt by method 'System.Web.Helpers.Json.Decode(System.String)' to access field 'System.Web.Helpers.Json._serializer' failed.
I'm using VS2010.
More detail:
System.FieldAccessException was caught
Message=Attempt by method 'System.Web.Helpers.Json.Decode(System.String)' to access field 'System.Web.Helpers.Json._serializer' failed.
Source=System.Web.Helpers
StackTrace:
at System.Web.Helpers.Json.Decode(String value)
at Components.DataCollection.ConvertTwitterStream.ConvertTweets() in C:\Users\Administrator\documents\visual studio 2010\Projects\ISMM\Components\DataCollection\ConvertTwitterStream.cs:line 35
InnerException:
Debugging calls to 'Json.Decode' fail when the Visual Studio hosting process is enabled (which is the default). I found it worked with the hosting process disabled or when running without the debugger.
The hosting process can be disabled for your project by following these instructions:
http://msdn.microsoft.com/en-us/library/ms185330.aspx
To support jbtule's answer, JsonFx v2 (http://github.com/jsonfx/jsonfx) makes this really easy. The example below shows a full round-trip with dynamic object being built from a JSON string and then serialized back into JSON.
string input = "{ \"foo\": true, \"array\": [ 42, false, \"Hello!\", null ] }";
dynamic value = new JsonReader().Read(input);
// verify that it works
Console.WriteLine(value.foo); // true
Console.WriteLine(value.array[0]); // 42
Console.WriteLine(value.array.Length); // 4
string output = new JsonWriter().Write(value);
// verify that it works
Console.WriteLine(output); // {"foo":true,"array":[42,false,"Hello!",null]}
JsonFx Supports several strategies of binding json to .net objects including dynamic objects.
https://github.com/jsonfx/jsonfx
I don't recall if Json.NET has support for dynamic objects yet, but it seems that you are able to do so with a little extra custom coding.
http://json.codeplex.com/
http://weblogs.asp.net/britchie/archive/2010/08/05/json-net-dynamic-extensions.aspx
Microsoft has added a Json helper class to Web Matrix Beta 2. Sample code here http://www.mikesdotnetting.com/Article/160/WebMatrix-Working-With-The-JSON-Helper
There seems to be a privileges issue when using Json.Decode in a Console App. Right-mouse click on your EXE and "Run as administrator..." and it should work.
I am not sure if there is a way to force Visual Studio to run the executable as administrator for debugging purposes or not.

Strong named assembly not working in my Visual C#

I have a project and I want to strong name the executable file. Using command-line compiling works well:
csc ... /keyfile:...
But I would like the IDE to do this, so I find this: in project property's `Signing' tab, there is a 'Sign the assembly' option. I tick it and direct to my key-pair file. After I lauch the debug, a FileNotFound exception shows up at an indifferent place: (my application uses serialization)
protected override Type d(Stream st)
{
BinaryFormatter bf = new BinaryFormatter();
return (Type)bf.Deserialize(st);
}
The application was doing right before I do this configureation.
You will need to appropriately configure the Binder property of the BinaryFormatter. Here is an example of how to do this: http://spazzarama.wordpress.com/2009/06/25/binary-deserialize-unable-to-find-assembly/
I assume you are reading back data that was written with the unsigned application. That data has now become incompatible, the Deserializer can't match the types.
I'm not sure how to fix this (quickly), but maybe you can confirm this first by writing and reading with the signed application, that should work.
It is a good idea to keep all your serialized types in a separate assembly.
Both of the responses were excellent. Adding my 1 pence to it: This is called TYPE FIDELITY which is possible only through Binary serialization and not in XML or any other type of serialization.

Categories

Resources