Nextrek
@ 859
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace csvTojSon { public static class Estensioni { public static string ToJSON<T>(this T obj) where T : class { System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T)); using (MemoryStream stream = new MemoryStream()) { serializer.WriteObject(stream, obj); return Encoding.Default.GetString(stream.ToArray()); } } public static T FromJSON<T>(this T obj, string json) where T : class { using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(json))) { System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T)); return serializer.ReadObject(stream) as T; } } public static string ToUTF8(this string s) { System.Text.Encoding utf_8 = System.Text.Encoding.UTF8; //string to utf byte[] utf = System.Text.Encoding.UTF8.GetBytes(s); //utf to string return System.Text.Encoding.UTF8.GetString(utf); } } } |