C# JSON import: could not convert string to byte array - c#

I've developed an application where is possible to import data from a JSON file.
This is the code I'm using to deserialize JSON:
var json_serializer = new JavaScriptSerializer();
var json_tpp = json.tpp;
ThermalPowerPlant tpp = json_serializer.Deserialize<ThermalPowerPlant>(json_tpp);
Everything was working fine, but lately I've modified the ThermalPowerPlant class to include a PDF that I save as byte array.
So, now, my JSON contains this field and if I try to import it again, application returns an exception that says it can not convert type String to Byte Array.
How can I handle this case?
EDIT
This is the byte array field in my JSON:
"Map": "JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9MZW5ndGggNTU1My9GaWx0ZXIvRmxhdGVEZWNvZGU+PnN0cmVhbQp4nJ1d23IcN5J951dU7JPmgeW6X/atR2xreoMi6WZbMeE3jkx7uUGwZYqeifDXL1CFS2YCmSgpHA4xC+dUnkQB2aebxeo/Lv5+uminop/64vTrRVVcTk1Zt/bnppvKarBB3XTlAtqfLn66+GP5vyn+Rw99uKj1yH/0T+a/4wd9wnLs9Tm7sukL5cJ+1qjni3sGvQyri2Y2wywYDnvwmohHEyEBntDezG0gLAEvpfVC+q4q65rHovEAZ4VjgIKqGN19V5cDYLiYVwTG1cXQzWU983A0HuBsARigiDymhqGvygGQXMyKguPqYqpaw2PhaDzA2RowQBF5TA1uNU9hvbUtkwCN+9XMwtF42CmTtE+ADAvk1vwCXX4SBLQhfVfraRwFNAYEQlowGFVeEKO3a+qyclgX8DoQIFx3loABYF0lhYNRBZXJ66OtzT/+2uhzDK2wRNZxf81ZOBoPK2pJJqwoLCbAubXiCEsgiGmDFHd9WTQGgPXFiccABZVl1o1juJjXhAChb7MEDAgEtggMUERiptc7kosFWRAQ2jdLwIBAYOvAAEUkZvq9I7mYl4UAYWeyBAwAe5+rAwMUkejqMP//ZKxTvfDqoltZg16RU3FSFz/8WBdzcfrt4t3d8fbueNifdsfD7X//7fR/nlLXXTkOgFMVp8+a2FhigcFj2URgefTdh58/XB9uT6ddsbve39/vbq6Ot/CkjWlwM6Vd1nNVVp1GrF4P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DAwNTkzOCAwMDAwMCBuIAowMDAwMDA1NjM2IDAwMDAwIG4gCjAwMDAwMDU5ODkgMDAwMDAgbiAKMDAwMDAwNjAzNCAwMDAwMCBuIAp0cmFpbGVyCjw8L1NpemUgOC9Sb290IDYgMCBSL0luZm8gNyAwIFIvSUQgWzxmNzAwMjI5ZDUxNzljNDZlNzNiMTFlZThiNTc0NmUxMj48ZjcwMDIyOWQ1MTc5YzQ2ZTczYjExZWU4YjU3NDZlMTI+XT4+CiVpVGV4dC01LjUuOApzdGFydHhyZWYKNjE5NgolJUVPRgo="
PS: I've also checked my JSON and it is correctly formatted.

Your field is Base64. You'll need to define your property as a string (because it is a string, even if it represents a byte array).
Then you can convert it like this:
var myArr = Convert.FromBase64String(tpp.map);

Replace JavaScriptSerializer with JSON.NET
And this code works perfectly:
class Program
{
public class SomeClass
{
public string SomeProperty { get; set; }
public byte[] ByteArrayProperty { get; set; }
}
static void Main()
{
SomeClass sc = new SomeClass()
{
SomeProperty = "la la la",
ByteArrayProperty = new byte[] {1, 2, 3}
};
string json = JsonConvert.SerializeObject(sc);
SomeClass newSC = JsonConvert.DeserializeObject<SomeClass>(json);
}
}
EDIT
json = "{\"SomeProperty\":\"la la la\",\"ByteArrayProperty\":\"JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9MZW5ndGggNTU1My9GaWx0ZXIvRmxhdGVEZWNvZGU+PnN0cmVhbQp4nJ1d23IcN5J951dU7JPmgeW6X/atR2xreoMi6WZbMeE3jkx7uUGwZYqeifDXL1CFS2YCmSgpHA4xC+dUnkQB2aebxeo/Lv5+uminop/64vTrRVVcTk1Zt/bnppvKarBB3XTlAtqfLn66+GP5vyn+Rw99uKj1yH/0T+a/4wd9wnLs9Tm7sukL5cJ+1qjni3sGvQyri2Y2wywYDnvwmohHEyEBntDezG0gLAEvpfVC+q4q65rHovEAZ4VjgIKqGN19V5cDYLiYVwTG1cXQzWU983A0HuBsARigiDymhqGvygGQXMyKguPqYqpaw2PhaDzA2Row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BvYmoKPDwvVHlwZS9DYXRhbG9nL1BhZ2VzIDQgMCBSPj4KZW5kb2JqCjcgMCBvYmoKPDwvUHJvZHVjZXIoaVRleHRTaGFycJIgNS41LjggqTIwMDAtMjAxNSBpVGV4dCBHcm91cCBOViBcKEFHUEwtdmVyc2lvblwpKS9DcmVhdGlvbkRhdGUoRDoyMDE2MDQxMTEzMDAwOCswMCcwMCcpL01vZERhdGUoRDoyMDE2MDQxMTEzMDAwOCswMCcwMCcpPj4KZW5kb2JqCnhyZWYKMCA4CjAwMDAwMDAwMDAgNjU1MzUgZiAKMDAwMDAwNTc1NyAwMDAwMCBuIAowMDAwMDA1ODUwIDAwMDAwIG4gCjAwMDAwMDAwMTUgMDAwMDAgbiAKMDAwMDAwNTkzOCAwMDAwMCBuIAowMDAwMDA1NjM2IDAwMDAwIG4gCjAwMDAwMDU5ODkgMDAwMDAgbiAKMDAwMDAwNjAzNCAwMDAwMCBuIAp0cmFpbGVyCjw8L1NpemUgOC9Sb290IDYgMCBSL0luZm8gNyAwIFIvSUQgWzxmNzAwMjI5ZDUxNzljNDZlNzNiMTFlZThiNTc0NmUxMj48ZjcwMDIyOWQ1MTc5YzQ2ZTczYjExZWU4YjU3NDZlMTI+XT4+CiVpVGV4dC01LjUuOApzdGFydHhyZWYKNjE5NgolJUVPRgo=\"}";
newSC = JsonConvert.DeserializeObject<SomeClass>(json);
Works perfectly. ByteArrayProperty is fill with bytes without errors.

Try the accepted solution at (e. g.) Deserializing JSON to abstract class
The asked question comes from another problem, but maybe you need to specify a TypeNameHandling for the JsonSerializerSettings, in order to make a difference between a string and a byte array. The interesting excerpt from the code there (adapted to your problem):
....
var jset = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All };
ThermalPowerPlant tpp = ...;
string json = JsonConvert.SerializeObject(tpp, jset);
....
ThermalPowerPlant tpp2 = (ThermalPowerPlant)JsonConvert.DeserializeObject(json, jset);
Didn't try to compile and maybe you are deserializing in another way, so you might have to improve this thing.

Related

Is there an easy way to convert a ListArray into a byte array in C#?

Is there a better way to convert a ListArray into a byte array in C#? The ListArray is coming from a dictionary object and contains a ListArray of objects where the underlying type is int. The only way I could get it to work was by looping thru the ListArray and individually inserting into the byte array. I was trying to get it to work with ToArray but I kept getting a cast error.
static void Main(string[] args)
{
int intLoop;
byte[] arrByte;
string strJson = "{\"Data\":[104,101,108,108,111,32,119,111,114,108,100]}";
Dictionary<string, object> dic = new Dictionary<string, object>();
JavaScriptSerializer js = new JavaScriptSerializer();
//deserialize json into dictionary object
dic = js.Deserialize<Dictionary<string, object>>(strJson);
//convert arraylist into byte array
intLoop = 0;
arrByte = new byte[((System.Collections.ArrayList)dic["Data"]).Count];
foreach (var s in (System.Collections.ArrayList)dic["Data"])
{
arrByte[intLoop] = Convert.ToByte(s);
intLoop++;
}
}
There's nothing wrong with your approach, but, if you prefer, you could use a LINQ one-liner instead:
ArrayList data = ...;
var arrByte = data.Cast<int>().Select(i => (byte)i).ToArray();
Note: Cast<int>() is required because your code uses the legacy ArrayList type. If your source type already implements IEnumerable<int> (for example, a List<int> or an int[]), you can skip this step.
The problem is that int[] and byte[] are the same when all numbers are less 255. If one of the numbers is large than 255 it will throw exeption during conversion.
If you need byte[]
string strJson = "{\"Data\":[104,101,108,108,111,32,119,111,114,108,100]}";
var byteArray = JsonConvert.DeserializeObject<RootData>(strJson);
class
public class RootData
{
public byte[] Data {get; set;}
}
That's how I would write it (using c# 9 language features)
public record MyDto(IEnumerable<int> Data);
var json = "{\"Data\":[104,101,108,108,111,32,119,111,114,108,100]}";
var document = JsonSerializer.Deserialize<MyDto>(json);
var bytes = document?.Data
.Select(i => (byte) i)
.ToArray();
Create data structure to represent the input data
Parse it with System.Text.Json JsonSerializer (the new standard json serializer)
Convert it with LINQ

C#'s version of JS's JSON [duplicate]

This question already has answers here:
Serialize and Deserialize Json and Json Array in Unity
(9 answers)
Closed 3 years ago.
I recently got into C# using Unity. I previously worked with JavaScript and know that they defiantly have their differences, but still have some similarities. I have used JSON with JS and it works great. Now with Unity, I want to store data of upcoming "stages" in JSON in an infinite runner game. But from my experience, JSON does not work nearly as well with C# as it does with JS. I have looked at Unity's JSON Utility but haven't figured out if it's possible to simply have a string and then convert it into an object which you could access like object.item.array[0].item which is how you'd do it in JS. Another thing that I looked at was this but as a novice to C#, I couldn't make heads or tails of it. So does C# have something like JSON, but its more integrated? I've used C# lists, can you get 3D lists with items and not just arrays? I know that they are very different languages, and what works well on one, might not on another.
I think closest to what you describe in your questions and the comments as
simply convert a JSON string into a JSONObject
would maybe be the good old SimpleJSON. Simply copy the SimpleJSON.cs and depending on your needs maybe SimpleJSONUnity.cs(provides some extensions for directly parsing to and from Vector2, Vector3, Vector4, Quaternion, Rect, RectOffset and Matrix4x4) somewhere into your Assets folder.
Then given the example json
{
"version": "1.0",
"data": {
"sampleArray": [
"string value",
5,
{
"name": "sub object"
}
]
}
}
you can simply access single fields like
using SimpleJSON;
...
var jsonObject = JSON.Parse(the_JSON_string);
string versionString = jsonObject["version"].Value; // versionString will be a string containing "1.0"
float versionNumber = jsonObject["version"].AsFloat; // versionNumber will be a float containing 1.0
string val = jsonObject["data"]["sampleArray"][0]; // val contains "string value"
string name = jsonObject["data"]["sampleArray"][2]["name"]; // name will be a string containing "sub object"
...
Using this you don't have to re-create the entire c# class representation of the JSON data which might sometimes be a huge overhead if you just want to access a single value from the JSON string.
However if you want to serialze and deserialize entire data structures you won't get happy using SimpleJSON. Given the example above this is how you would use Unity's JsonUtility
Create the c# class representation of the data yu want to store. In this case e.g. something like
[Serializable]
public class RootObject
{
public string version = "";
public Data data = new Data();
}
[Serializable]
public class Data
{
public List<object> sampleArray = new List<object>();
}
[Serializeable]
public class SubData
{
public string name = "";
}
Then fill it with values and parse it to JSON like
var jsonObject = new RootObject()
{
version = "1.0",
data = new Data()
{
sampleArray = new List<object>()
{
"string value",
5,
new SubData(){ name = "sub object" }
}
}
};
var jsonString = JsonUtility.ToJson(jsonObject);
And to convert it back to c# either if jsonObject was not created yet
jsonObject = JsonUtility.FromJson<RootObject>(jsonString);
otherwise
JsonUtility.FromJsonOverwrite(jsonString, jsonObject);
JSON is just how objects are represented in JavaScript. While C# can use JSON, you'll probably have a much easier time defining your own custom classes. Using classes, you can define all the properties you'll need, string them together into a list, etc.. Hope this helps.

How can i define a class to deserialze binary file

I would like to know how can i define a class for a binary file which contains objects with float array of size 19.
please see the attached picture of how data looks like in Hex Editor Neo. when displayed as float
i have tried following but no luck. please tell what i am doing wrong here.
[ProtoContract]
public class ChannelData
{
[ProtoMember(1)]
public List<float> array = new List<float>(19);
}
using (var file = File.OpenRead("0.cnl"))
{
ChannelData newchannel = Serializer.Deserialize<ChannelData>(file);
}
I am using protobuff for deserialize. Thankyou!
that binary file is not serialized using any binary serializer its raw file.
so i think it can not be deserialize using any deserializer? may be need to do with BitConverter but have no idea..
You can load floats to the list, then split it to Channels, like
public static unsafe List<float> LoadToList(byte[] bytes)
{
var list = new List<float>();
var step = sizeof (float);
for (int i = 0; i < bytes.Length; i += step)
{
fixed (byte* pbyte = &bytes[i])
{
list.Add(*((float*)pbyte));
}
}
return list;
}

Parse a string containing several json objects into something more convenient

The crux of the problem here is that I don't know any C#, yet find myself adding a feature to some test infrastructure which happens to be written in C#. I suspect this question is entirely trivial and beg your patience in answering. My colleagues who originally wrote this stuff are all out of the office.
I am parsing a string representing one or more json objects. So far I can get the first object, but can't work out how to access the remainder.
public class demo
{
public void minimal()
{
// Note - the input is not quite json! I.e. I don't have
// [{"Name" : "foo"}, {"Name" : "bar"}]
// Each individual object is well formed, they just aren't in
// a convenient array for easy parsing.
// Each string representation of an object are literally concatenated.
string data = #"{""Name"": ""foo""} {""Name"" : ""bar""}";
System.Xml.XmlDictionaryReader jsonReader =
JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(data),
new System.Xml.XmlDictionaryReaderQuotas());
System.Xml.Linq.XElement root = XElement.Load(jsonReader);
Assert.AreEqual(root.XPathSelectElement("//Name").Value, "foo");
// The following clearly doesn't work
Assert.AreEqual(root.XPathSelectElement("//Name").Value, "bar");
}
}
I'm roughly at the point of rolling enough of a parser to work out where to split the string by counting braces but am hoping that the library support will do this for me.
The ideal end result is a sequential datastructure of your choice (list, vector? don't care) containing one System.Xml.Linq.XElement for each json object embedded in the string.
Thanks!
edit: Roughly viable example, mostly due to George Richardson - I'm playing fast and loose with the type system (not sure dynamic is available in C#3.0), but the end result seems to be predictable.
public class demo
{
private IEnumerable<Newtonsoft.Json.Linq.JObject>
DeserializeObjects(string input)
{
var serializer = new JsonSerializer();
using (var strreader = new StringReader(input))
{
using (var jsonreader = new JsonTextReader(strreader))
{
jsonreader.SupportMultipleContent = true;
while (jsonreader.Read())
{
yield return (Newtonsoft.Json.Linq.JObject)
serializer.Deserialize(jsonreader);
}
}
}
}
public void example()
{
string json = #"{""Name"": ""foo""} {""Name"" : ""bar""} {""Name"" : ""baz""}";
var objects = DeserializeObjects(json);
var array = objects.ToArray();
Assert.AreEqual(3, array.Length);
Assert.AreEqual(array[0]["Name"].ToString(), "foo");
Assert.AreEqual(array[1]["Name"].ToString(), "bar");
Assert.AreEqual(array[2]["Name"].ToString(), "baz");
}
}
You are going to want to use JSON.net for your actual deserialization needs. The big problem I see here is that your json data is just being concatenated together which means you are going to have to extract each object from the string. Luckily json.net's JsonReader has a SupportMultipleContent property which does just this
public void Main()
{
string json = #"{""Name"": ""foo""} {""Name"" : ""bar""} {""Name"" : ""baz""}";
IEnumerable<dynamic> deserialized = DeserializeObjects(json);
string name = deserialized.First().Name; //name is "foo"
}
IEnumerable<object> DeserializeObjects(string input)
{
JsonSerializer serializer = new JsonSerializer();
using (var strreader = new StringReader(input)) {
using (var jsonreader = new JsonTextReader(strreader)) {
jsonreader.SupportMultipleContent = true;
while (jsonreader.Read()) {
yield return serializer.Deserialize(jsonreader);
}
}
}
}

Creating a double serialized JSON object in REST SOE C#

I'm not sure if I'm double-serializing my JSON object, but the output results in an unwanted format. I'm exposing the REST endpoint via an ArcGIS Server Object Extension (REST SOE). I've also recently implemented JSON.Net which essentially allowed me to remove several lines of code.
So here is the handler, which is the core piece creating the data for the service (for you non GIS peeps).
private byte[] SearchOptionsResHandler(NameValueCollection boundVariables, string outputFormat, string requestProperties, out string responseProperties)
{
responseProperties = null;
JsonObject result = new JsonObject();
// Towns
DataTable dataTableTowns = GetDataTableTowns();
String jsonStringTowns = JsonConvert.SerializeObject(dataTableTowns);
result.AddString("Towns", jsonStringTowns);
// GL_Description
DataTable dataTableGLDesc = GetDataTableGLDesc();
String jsonStringGLDesc = JsonConvert.SerializeObject(dataTableGLDesc);
result.AddString("GLDesc", jsonStringGLDesc);
return Encoding.UTF8.GetBytes(result.ToJson());
}
The result is ugly scaped JSON:
{
"Towns": "[{\"Column1\":\"ANSONIA\"},{\"Column1\":\"BETHANY\"},{\"Column1\":\"BLOOMFIELD\"}]",
"GLDesc": "[{\"Column1\":\"Commercial\"},{\"Column1\":\"Industrial\"},{\"Column1\":\"Public\"}]"
}
Is it because I'm double serializing it somehow? Thanks for looking this over.
Yes, you are double serializing it. It's right there in your code.
For each of your data tables, dataTableTowns and dataTableGLDesc, you are calling JsonConvert.SerializeObject() to convert them to JSON strings, which you then add to a result JsonObject. You then call ToJson() on the result, which presumably serializes the whole thing to JSON again.
JsonObject is not part of Json.Net, while JsonConvert is, so I would recommend using one or the other. Generally, you just want to accumulate everything into a single result object and then serialize the whole thing once at the end. Here is how I would do it with Json.Net using an anonymous object to hold the result:
var result = new
{
Towns = GetDataTableTowns(),
GLDesc = GetDataTableGLDesc()
};
string json = JsonConvert.SerializeObject(result);
return Encoding.UTF8.GetBytes(json);
Here is the output:
{
"Towns":[{"Column1":"ANSONIA"},{"Column1":"BETHANY"},{"Column1":"BLOOMFIELD"}],
"GLDesc":[{"Column1":"Commercial"},{"Column1":"Industrial"},{"Column1":"Public"}]
}

Categories

Resources