Object to base 64 JSON file - c#

I have an object T. I need to send the file representation of it into a web service. Without saving the file to a temp file.
WebService method:
myClient.SendFile(
new SendFileData{
id = "1",
fileName = "Foo",
fileType = "json",
fileContent = base64, // string Base64 variable here
}
To get the Base64 of a file I use :
public static string FileToBase64(string path) => FileToBase64(File.ReadAllBytes(path));
public static string FileToBase64(Byte[] bytes) => Convert.ToBase64String(bytes);
I have made those method to work on a temp file stored in a directory. saving the Json to file using :
using (StreamWriter file = File.CreateText(tempPath))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, _data);
}
And reading it like:
Directory.GetFiles(directory).Where().Select(x=> new {
fi = new FileInfo(f),
name = Path.GetFileNameWithoutExtension(f),
ext = Path.GetExtension(f).Trim('.'),
content = FileBase64Helper.FileToBase64(f)
})
I try to make a in memory file and convert it to b64 like:
public void Demo()
{
var myObject = new CustomType { Id = 1, Label = "Initialisation of a custom object" };
var stringRepresentation =
JsonConvert.SerializeObject(myObject, Formatting.Indented, new JsonSerializerSettings { });
SendFileData dataProjection = new SendFileData { };
var result = FakeClient.SendFile(dataProjection);
}
public class CustomType
{
public string Label { get; set; }
public int Id { get; set; }
}
public static class FakeClient
{
public static bool SendFile(SendFileData data) => true;
}
public class SendFileData
{
public string id { get; set; }
public string fileName { get; set; }
public string fileType { get; set; }
public string fileContent { get; set; }
}
Comparaison between direct convertion and FileReadByte:
var myObject = new CustomType { Id = 1, Label = "Initialisation of a custom object" };
var stringRepresentation = JsonConvert.SerializeObject(myObject, Formatting.Indented, new JsonSerializerSettings { });
var directSerialization = Convert.ToBase64String(Encoding.UTF8.GetBytes(stringRepresentation));
var tempPath = #"test.json";
using (StreamWriter file = File.CreateText(tempPath))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, myObject);
}
var fromFile = FileBase64Helper.FileToBase64(tempPath);
SendFileData dataProjection = new SendFileData { };
var result = FakeClient.SendFile(dataProjection);
Direct serialization:
ew0KICAiTGFiZWwiOiAiSW5pdGlhbGlzYXRpb24gb2YgYSBjdXN0b20gb2JqZWN0IiwNCiAgIklkIjogMQ0KfQ==
From file
eyJMYWJlbCI6IkluaXRpYWxpc2F0aW9uIG9mIGEgY3VzdG9tIG9iamVjdCIsIklkIjoxfQ==

If you're question is as follows and if i understand it correctly:
"Does File.ReadAllBytes add additional information about the file in it's return value, or is the return value equal to just having the content of the file as a byte array?"
Answer:
According to Microsoft Documentation, it will return the content of the file as a byte array.
Hope this helps!
Source: https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readallbytes?view=netframework-4.7.2
EDIT: Decoding the base64, i found this:
The difference in the base64 encoded values is the json formatting, nothing else :D
Direct serialization:
From file:

Related

How can I convert Serialized stream to a string?

I need to serialize and deserialize a list and write it on a JSON file to use later.
I successfully desirialize the file but failed to write after serialization. How can I do that?
Here is the code I have written.
StorageFile savedFile = await storageFolder.GetFileAsync("SavedData.json");
string text = await FileIO.ReadTextAsync(savedFile);
var serializer = new DataContractJsonSerializer(typeof(DataFormat));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(text));
List<DataFormat> data = (List<DataFormat>)serializer.ReadObject(ms);
if (data == null)
{
data = new List<DataFormat>();
}
data.Add(new DataFormat
{
firstName = fnbox.Text,
lastName = lnbox.Text,
country = cbox.Text
});
MemoryStream stream = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DataFormat));
ser.WriteObject(stream, data);
DataFormat Class -
[DataContract]
public class DataFormat : IEquatable<DataFormat>
{
[DataMember]
public string firstName{ get; set; }
[DataMember]
public string lastName { get; set; }
[DataMember]
public string country { get; set; }
public bool Equals(DataFormat other)
{
if (other == null)
{
return false;
}
return (firstName.Equals(other.firstName));
}
}
Additionally If there is any way to just add lines into an existing
file without replacing all the text, please let me know.
See if the following code is what you need. I'm not so sure whether you mean you don't understand how to write to stream.
private async void Button_Click(object sender, RoutedEventArgs e)
{
Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile sampleFile =await storageFolder.GetFileAsync("sample.json");
DataFormat data = new DataFormat();
data.firstName = "Barry";
data.lastName = "Wang";
data.country = "China";
Stream mystream = await sampleFile.OpenStreamForWriteAsync();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DataFormat));
ser.WriteObject(mystream, data);
}
Here what you need, you can use LosFormatter class - see
https://weblog.west-wind.com/posts/2006/Oct/13/LosFormatter-for-easy-Serialization

How to deserialize JSON using Newtonsoft Deserialize C#

I am trying to deserialize JSON file and want to assign to object ScanResult. var text showing all the values but scanresult showing null some null values. https://gyazo.com/ff2ce386f845394c458a88d43a1f30d8
please suggest if I am missing something.
//MY jSon File SCAN Test 1-1543045410222.json 's code
{
"at": 1543045410222,
"i": 1000,
"s": {
"Sensor1": ["OFF"],
"Sensor2": ["OFF"],
"DataReady1": ["OFF"],
"DataReady2": ["OFF"],
"CV1": [5.0],
"CV2": [6.0]
}
}
//ViewModel Code is as below:
public void ResendScanResult()
{
var ScanActivities = scanActivityManager.GetAll();
foreach (var item in ScanActivities)
{
var scanName = item.ScanName;
var dir = _dataFilePath + scanName + "\\";
var jsonFileName = string.Format("{0}{1}-{2}.json", dir, scanName, item.ScanDateEpoch);
string fileName = Path.GetFileName(jsonFileName);
// ScanResult scanResult = new ScanResult();
var text = File.ReadAllText(jsonFileName);
//var scanResults = JsonConvert.DeserializeObject<ScanResult>(text);
Common.Model.ScanResult scanResult = JsonConvert.DeserializeObject<Common.Model.ScanResult>(text);
var Mvm = MonitorViewModel.Instance;
// TargetProvider target = Mvm.GetTargetProvider(scanResult);
// Mvm.PublishToServer(target, scanResult);
}
}
and my scanRescult class code is as below :
namespace ABX.Common.Model
{
public class ScanResult
{
public ScanResult()
{
At = DateTimeOffset.Now.ToUnixTimeMilliseconds();
Interval = 1;
}
public string Name { get; set; }
public long At { get; set; }
public long Interval { get; set; }
public JObject Values { get; set; }
public string FileName { get; set; }
public JObject ToJson()
{
JObject json = new JObject
{
{ "at", At },
{ "i", Interval },
{ "s", Values }
};
return json;
}
Either rename your class properties to match your JSON, rename your JSON to match your class properties, or implement a custom JsonConverter, where you can implement arbitrary mapping.

Reading content from a file stream c#

I have a web api service that accepts file uploads. The content of the file is a small JSON string like the following:
{ "name":"John", "age":31, "city":"New York" }
How do I get the file content while it is a stream instead of having to save the stream as a file on the web server and then open the file?
The following is my snippet:
byte[] fileData = null;
using(var binaryReader = new BinaryReader(httpRequest.Files[0].InputStream))
{
fileData = binaryReader.ReadBytes(httpRequest.Files[0].ContentLength);
}
I'm using the 4.0 .NET Framework
You can use a StreamReader class. Try this code:
using (var reader = new StreamReader(httpRequest.Files[0].InputStream))
{
var content = reader.ReadToEnd();
var json = JObject.Parse(content);
var name = json["name"];
}
Another option is to create a class for your json (manually or by http://json2csharp.com/):
public class Person
{
public string name { get; set; }
public int age { get; set; }
public string city { get; set; }
}
Then change your code to this:
using (var reader = new StreamReader(httpRequest.Files[0].InputStream))
{
var content = reader.ReadToEnd();
var person = JsonConvert.DeserializeObject<Person>(content);
var name = person.name;
}

Serialization Fail on list of lists using DataContractSerializer

I have some class that has a property of type List<object> I need to serialize that class to XML file using DataContractSerializer.
The serialization fails on ArgumentException when the object is a List<T>/IEnumerator<T>exception message:
Invalid name character in
'System.Collections.Generic.List`1[[MyProj.Result, MyProj,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.
Here is the code sample that fails
The Class that takes the List<object>
[DataContract(IsReference = true)]
public class RecoveryMethodData
{
[DataMember]
public List<object> Parameters { get; set; }
public RecoveryMethodData()
{
Parameters = new List<object>();
}
public static void SerializeToFile(RecoveryMethodData recoveryMethodData, string fileName)
{
var encoding = Encoding.UTF8;
using (var fileWriter = new XmlTextWriter(fileName, encoding))
{
fileWriter.Formatting = Formatting.Indented;
// use SharedTypeResolver for deserializing assistance.
var serializer = new DataContractSerializer(typeof(RecoveryMethodData), null, int.MaxValue, false, true, null, new SharedTypeResolver());
serializer.WriteObject(fileWriter, recoveryMethodData);
}
}
}
Here is the usage:
private void TestSerialization()
{
var methodData = new RecoveryMethodData();
var result = new Result() {Message = "wow", Pass = true, FileName = "somefile "};
methodData.Parameters.Add(result);
methodData.Parameters.Add(true);
var list1 = new List<Result>();
list1.Add(new Result(){FileName = "in list1", Message = "in l 1"});
list1.Add(new Result(){FileName = "in list2", Message = "in l 2"});
methodData.Parameters.Add(list1);
RecoveryMethodData.SerializeToFile(methodData,#"C:\serialization_result.xml");
}
public class Result
{
public string Message { get; set; }
public string FileName { get; set; }
}
If I do not add list1 into the methodData.Parameters there is no problem serializing the methodDatad object.
One big limitation is that I can't know in advance which kind of objects will be added to the Parameters property (that is why it is a list of objects)
In order to DataContractSerializer to serialize an object, it shall know the types of all datamembers. In your case, you do not define a specific type but an object type. Try changing the definition of
public List<object> Parameters { get; set; }
to something like:
public List<IMyObject> Parameters { get; set; }
Note that, all of your objects which you are trying to add to the parameters list shall inherit IMyObject interface.
Update: I refactored your code up to some point (still in a bad shape) and it seems working, please have a try;
public class Tester
{
public Tester()
{
this.TestSerialization();
}
public void SerializeToFile(RecoveryMethodData recoveryMetaData,string fileName)
{
var encoding = Encoding.UTF8;
using (var fileWriter = new XmlTextWriter(fileName, encoding))
{
fileWriter.Formatting = Formatting.Indented;
// use SharedTypeResolver for deserializing assistance.
var serializer = new DataContractSerializer(typeof(RecoveryMethodData),new List<Type>(){typeof(bool),typeof(Result),typeof(List<Result>)});
serializer.WriteObject(fileWriter,recoveryMetaData);
}
}
private void TestSerialization()
{
var methodData = new RecoveryMethodData();
var result = new Result() { Message = "wow", Pass = true, FileName = "somefile " };
methodData.Add(result);
methodData.Add(true);
var list1 = new List<Result>();
list1.Add(new Result() { FileName = "in list1", Message = "in l 1" });
list1.Add(new Result() { FileName = "in list2", Message = "in l 2" });
methodData.Add(list1);
SerializeToFile(methodData, #"C:\serialization_result.xml");
}
}
public class Result
{
public string Message { get; set; }
public string FileName { get; set; }
public bool Pass { get; set; }
}
public class RecoveryMethodData : List<object>
{
}

XML Deserialization

I have an xml file which is in this format
"<rundate>
<rundateItem>
<LeaveCreditingMonth>2</LeaveCreditingMonth>
<LeaveCreditingYear>2010</LeaveCreditingYear>
<IncludeNoTimesheet>True</IncludeNoTimesheet>
</rundateItem>
</rundate>"
in case i want to deserialize this xml file, what should be the format of the class or the target object of my deserialization?
Currently my class looks like this:
public class rundate
{
string _leaveCreditingMonth;
string _leaveCreditingYear;
string _includeNoTimesheet;
public string LeaveCreditingMonth {get{return _leaveCreditingMonth;}set{ _leaveCreditingMonth = value;}}
public string LeaveCreditingYear {get{return _leaveCreditingYear;}set{ _leaveCreditingYear = value;}}
public string IncludeNoTimesheet {get{return _includeNoTimesheet;}set{ _includeNoTimesheet = value;}}
}
Your class can stay as is (obviously you should change the data types to be appropriate though) - since you have rundate nested in your XML (which implies there can be more than one) I would suggest adding a collection class as follows:
[XmlRoot("rundate")]
public class RundateCollection
{
[XmlElement("rundateItem")]
public List<rundate> Rundates { get; set; }
}
You can test serializing/deserializing your class with your XML as follows:
XmlSerializer serializer = new XmlSerializer(typeof(RundateCollection));
StringWriter sw = new StringWriter();
rundate myRunDate = new rundate() { LeaveCreditingMonth = "A", IncludeNoTimesheet = "B", LeaveCreditingYear = "C" };
RundateCollection ra = new RundateCollection() { Rundates = new List<rundate>() { myRunDate } };
serializer.Serialize(sw, ra);
string xmlSerialized = sw.GetStringBuilder().ToString();
string xml = File.ReadAllText(#"test.xml");
StringReader sr = new StringReader(xml);
var rundateCollection = serializer.Deserialize(sr);
You will see that the collection class is successfully deserialized from your XML and contains one list item of type runlist.
I would design the class like so:
public class Rundate
{
public int LeaveCreditingMonth { get; set;}
public int LeaveCreditingYear { get; set; }
public bool IncludeNoTimesheet { get; set; }
}
Then you can deserialize it like this:
var serializer = new XmlSerializer(typeof(List<Rundate>));
using (var fs = new FileStream("yourfile.xml", FileMode.Open))
{
using (var reader = new XmlTextReader(fs))
{
var rundates = (List<Rundate>)serializer.Deserialize(reader);
}
}

Categories

Resources