protected override void OnCreate (Bundle savedInstanceState) { base.OnCreate (savedInstanceState); SetContentView (Resource.Layout.main); var textView = FindViewById<TextView> (Resource.Id.textView); // create an object var account = new Account ("Xamarin", "Corporate"); // create the serializer var gson = new GsonBuilder () .SetPrettyPrinting () .Create (); // serialize the object into json var json = gson.ToJson (account); // deserialize the json into the object var clazz = Java.Lang.Class.FromType (typeof(Account)); var newAccount = (Account)gson.FromJson (json, clazz); textView.Text = string.Join (System.Environment.NewLine, new [] { json, "Name == 'Xamarin': " + (newAccount.Name == "Xamarin"), "Type == 'Corporate': " + (newAccount.Type == "Corporate") }); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.main); var textView = FindViewById <TextView> (Resource.Id.textView); // create an object var account = new Account("Xamarin", "Corporate"); // create the serializer var gson = new GsonBuilder() .SetPrettyPrinting() .Create(); // serialize the object into json var json = gson.ToJson(account); // deserialize the json into the object var clazz = Java.Lang.Class.FromType(typeof(Account)); var newAccount = (Account)gson.FromJson(json, clazz); textView.Text = string.Join(System.Environment.NewLine, new [] { json, "Name == 'Xamarin': " + (newAccount.Name == "Xamarin"), "Type == 'Corporate': " + (newAccount.Type == "Corporate") }); }
//public static string EncodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality) //{ // System.IO.MemoryStream byteArrayOS = new System.IO.MemoryStream(); // image.Compress(compressFormat, quality, byteArrayOS); // return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT); //} //public static Bitmap decodeBase64(String input) //{ // byte[] decodedBytes = Base64.Deco(input, 0); // return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length); //} public static T Cast <T>(DataSnapshot obj) where T : class { HashMap map = obj.Value.JavaCast <HashMap>(); Gson gson = new GsonBuilder().Create(); string jsonObj = gson.ToJson(map); return(JsonConvert.DeserializeObject <T>(jsonObj) as T); }
public void OnDataChange(DataSnapshot snapshot) { //"Data received for user orders"); var gson = new GsonBuilder().SetPrettyPrinting().Create(); var json = gson.ToJson(snapshot.Value); // Gson extention method obj -> string Formatted_Output("Data received for user order json ", json); User user = JsonConvert.DeserializeObject<User>(json); //Newtonsoft.Json extention method string -> object //now the user is a fully populated object with very little work }
public void OnDataChange(DataSnapshot snapshot) { if (snapshot.Exists()) { Gson gson = new GsonBuilder().Create(); HashMap map = snapshot.Value.JavaCast <HashMap>(); string tripdata = gson.ToJson(map); MessagingCenter.Send <object, string>(this, "tripin", tripdata); } }
void IChildEventListener.OnChildAdded(DataSnapshot snapshot, string previousChildName) { HashMap dataHashMap = snapshot.Value.JavaCast <HashMap>(); Gson gson = new GsonBuilder().Create(); string chatItemDaataString = gson.ToJson(dataHashMap); // Try to deserialize : try { T chatItems = JsonConvert.DeserializeObject <T>(chatItemDaataString); action("temp", chatItems); } catch { } }
void IValueEventListener.OnDataChange(DataSnapshot snapshot) { string chatItemsString = snapshot.Value.ToString(); object chatItemsObject = snapshot.Value; HashMap javaObjChatItems = snapshot.Value.JavaCast <HashMap>(); Gson gson = new GsonBuilder().SetPrettyPrinting().Create(); string chatItemDaataString = gson.ToJson(javaObjChatItems); Dictionary <string, ChatItem> chatItems = JsonConvert.DeserializeObject <Dictionary <string, ChatItem> >(chatItemDaataString); //// create the serializer //var gson = new GsonBuilder() // .SetPrettyPrinting() // .Create(); //String mapAsJson = JsonConvert.SerializeObject(javaObjChatItems); //Dictionary<string, ChatItem> chatItems = ObjectTypeHelper.Cast<Dictionary<string, ChatItem>>(snapshot.Value); //Dictionary<string, ChatItem> chatItems = snapshot.Value as Dictionary<string, ChatItem>; //snapshot. //HashMap javaObjChatItems = snapshot.Value.JavaCast<HashMap>(); //String mapAsJson = new ObjectMapper().writeValueAsString(javaObjChatItems); //ChatItem chatItem = ObjectTypeHelper.Cast<ChatItem>(javaObjChatItems.Get("item1")); //Dictionary<string, ChatItem> chatItems = snapshot.GetValue(ChatItem.); //JSON.Stringify(chatItemsObject); //Dictionary<string, ChatItem> chatItems = JsonConvert.DeserializeObject<Dictionary<string, ChatItem>>(chatItemDaataString); //Dictionary<string, ChatItem> chatItems = snapshot.GetValue(type of Dictionary);//JsonConvert.DeserializeObject<Dictionary<string, ChatItem>>(chatItemsString); //GenericTypeIndicator t = new GenericTypeIndicator() { }; //NSDictionary chatItemData = snapshot.GetValue<NSDictionary>(); //string test = snapshot.Key; //NSError error = null; //string chatItemDaataString = NSJsonSerialization.Serialize(chatItemData, NSJsonWritingOptions.PrettyPrinted, out error).ToString(); //ChatItem chatItem = JsonConvert.DeserializeObject<ChatItem>(chatItemDaataString); //BigSlickChatPage.messages.Add(chatItems.First().Value); //throw new NotImplementedException(); }
void IValueEventListener.OnDataChange(DataSnapshot snapshot) { if (snapshot.Exists() && snapshot.HasChildren) { HashMap dataHashMap = snapshot.Value.JavaCast <HashMap>(); Gson gson = new GsonBuilder().Create(); string data = gson.ToJson(dataHashMap); // Try to deserialize : try { T chatItems = JsonConvert.DeserializeObject <T>(data); action(chatItems); } catch { } } else { T item = default(T); action(item); } }