Here is the Json Result :
"[{\"EmpId\":1,\"Full_Name\":\"Parth Dave\",\"City\":\"\"},{\"EmpId\":2,\"Full_Name\":\"Chirag\",\"City\":\"\"},{\"EmpId\":3,\"Full_Name\":\"Jay\",\"City\":\"\"}]"
Here is my code:
public string GetProducts()
{
var empQuery = from emp in objEmployeeEntities.Employees
select new EmployeeClass() { EmpId = emp.EmpId, Full_Name = emp.Full_Name };
var settings = new JsonSerializerSettings() { ContractResolver = new NullToEmptyStringResolver() };
var str = JsonConvert.SerializeObject(empQuery.ToList(), settings);
return str;
}
public class NullToEmptyStringValueProvider : IValueProvider
{
PropertyInfo _MemberInfo;
public NullToEmptyStringValueProvider(PropertyInfo memberInfo)
{
_MemberInfo = memberInfo;
}
public object GetValue(object target)
{
object result = _MemberInfo.GetValue(target);
if (_MemberInfo.PropertyType == typeof(string) && result == null) result = "";
return result;
}
public void SetValue(object target, object value)
{
_MemberInfo.SetValue(target, value);
}
}
public class NullToEmptyStringResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
return type.GetProperties()
.Select(p =>
{
var jp = base.CreateProperty(p, memberSerialization);
jp.ValueProvider = new NullToEmptyStringValueProvider(p);
return jp;
}).ToList();
}
}
Related
Hi I have a Login API which I am using to login though my xamarin.forms app.
I will post my username and password and in return I am getting some data.Now Iam facing some issues at deserialization of json. Iam getting data at my resultJson but I cant deserialize it. Help me.
My Json :
[
{
"Result":true,
"ID":"fc938df0",
"LoginName":"test",
"UserName":"test",
"ConnectionString":"MSSQLSERVER;Initial Catalog=Test1;User ID=db;Password=db#2018",
"UserProfileID":"fc938df0"
}
]
My API Call class which have deserialization of Json.
public T APICallResult<T>()
{
try
{
Device.BeginInvokeOnMainThread(() =>
{
if (loadingIndicator != null)
{
loadingIndicator.IsRunning = true;
loadingIndicator.IsVisible = true;
}
});
var client = new HttpClient { BaseAddress = baseAddress };
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var req = new HttpRequestMessage(HttpMethod.Post, apiurl);
req.Content = new StringContent(postdata, Encoding.UTF8, "application/json");
string stringObtained = "";
Task<string> task = Task.Run(async () => await Threading(client, req));
task.Wait();
stringObtained = task.Result;
var jsonObtained = Regex.Unescape(stringObtained);
int startIndex = jsonObtained.IndexOf('[');
int endIndex = jsonObtained.LastIndexOf(']');
int length = endIndex - startIndex + 1;
var resultJSON = jsonObtained.Substring(startIndex, length);
T resultObject;//Generic type object
try
{
//**Deserializing**
resultObject = JsonConvert.DeserializeObject<T>(resultJSON);//, settings);
removeLoadingAnimation();
return resultObject;
}
catch (Exception e)
{
List<ErrorMessageData> errorMessages = JsonConvert.DeserializeObject<List<ErrorMessageData>>(resultJSON);
errorMessage = errorMessages[0];
removeLoadingAnimation();
return default(T);
}
}
catch (Exception e)
{
errorMessage = new ErrorMessageData();
errorMessage.Flag = false;
errorMessage.Message = e.Message;
removeLoadingAnimation();
return default(T);
}
}
My API call at login class
string postdataForLogin = "{\"userName\":\"" + userName.Text + "\",\"password\":\"" + password.Text + "\",\"RequestURL\":\"" + CommonValues.RequestURL + "\"}";
APICall callForLogin = new APICall("/API/LoginMobile/HomeLogin", postdataForLogin, loadingIndicator);
try
{
List<LoginData> resultObjForLogin = callForLogin.APICallResult <List<LoginData>>();
if (resultObjForLogin != null)
{
LoginData loginData = new LoginData();
loginData = resultObjForLogin[0];
Settings.userID = loginData.UserProfileID;
Settings.connectionString = loginData.ConnectionString;
if (loginData.Result)
{
Device.BeginInvokeOnMainThread(async () =>
{
Navigation.InsertPageBefore(new MainPage(), this);
await Navigation.PopAsync();
});
}
My DataModel
public class LoginData
{
public bool Result { get; set; }
public string ID { get; set; }
public string UserProfileID { get; set; }
public string LoginName { get; set; }
public string UserName { get; set; }
public string ConnectionString { get; set; }
}
Its seems strange, My problem solved after downgrading my xamarin.forms from latest version to pre release 4.0.0.169046- pre5. I think its a xamarin forms bug
I created a map with custom marker with real-time location update. But after adding a new pin to the map its not applying the custom renderer. If I zoomed-in or zoomed-out on the map its applying that custom renders for markers. Here is my code.
This Code is in Xamarin.Droid project
public class CustomMapRenderer : MapRenderer, IOnMapReadyCallback, GoogleMap.IInfoWindowAdapter
{
GoogleMap map;
List<Position> routeCoordinates;
List<CustomPin> customPins;
Action<CustomPin> onInfoWindowClicked;
public void OnMapReady(GoogleMap googleMap)
{
map = googleMap;
//map.InfoWindowClick += OnInfoWindowClick;
map.SetInfoWindowAdapter(this);
var polylineOptions = new PolylineOptions();
polylineOptions.InvokeColor(Android.Graphics.Color.Blue);
foreach (var position in routeCoordinates)
{
polylineOptions.Add(new LatLng(position.Latitude, position.Longitude));
}
map.AddPolyline(polylineOptions);
}
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Xamarin.Forms.View> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
map.InfoWindowClick -= OnInfoWindowClick;
// Unsubscribe
}
if (e.NewElement != null)
{
var formsMap = (CustomMap)e.NewElement;
routeCoordinates = formsMap.RouteCoordinates;
customPins = formsMap.CustomPins;
onInfoWindowClicked = formsMap.OnInfoWindowClicked;
((Android.Gms.Maps.MapView)Control).GetMapAsync(this);
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (map != null)
{
map.Clear();
foreach (var pin in customPins)
{
var marker = new MarkerOptions();
marker.SetPosition(new LatLng(pin.Pin.Position.Latitude, pin.Pin.Position.Longitude));
marker.SetTitle(pin.Id.ToString());
marker.SetSnippet(pin.Pin.Address);
if(pin.UserType == global::Common.Models.UserType.Driver)
{
marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.car));
}
else if (pin.UserType == global::Common.Models.UserType.Rider)
{
marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.person));
}
map.AddMarker(marker);
}
}
}
void OnInfoWindowClick(object sender, GoogleMap.InfoWindowClickEventArgs e)
{
onInfoWindowClicked(GetCustomPin(e.Marker));
}
private CustomPin GetCustomPin(Marker marker)
{
return customPins.Find(x => x.Id.ToString() ==marker.Title.ToString());
}
public Android.Views.View GetInfoContents(Marker marker)
{
var inflater = Android.App.Application.Context.GetSystemService(Context.LayoutInflaterService) as Android.Views.LayoutInflater;
if (inflater != null)
{
Android.Views.View view;
var customPin = GetCustomPin(marker);
if (customPin == null)
{
throw new Exception("Custom pin not found");
}
view = inflater.Inflate(Resource.Layout.MapInfoWindow, null);
var infoImage = view.FindViewById<ImageView>(Resource.Id.markerInfoImage);
var infoTitle = view.FindViewById<TextView>(Resource.Id.markerInfoTitle);
var infoSummary = view.FindViewById<TextView>(Resource.Id.markerInfoSummary);
System.IO.Stream ims = Context.Assets.Open(customPin.Image);
// load image as Drawable
Drawable d = Drawable.CreateFromStream(ims, null);
// set image to ImageView
infoImage.SetImageDrawable(d);
//File file = new File(customPin.Image);
//var image = Android.Net.Uri.FromFile(file);
//var resource=ResourceManager.GetDrawableByName("driverLogActive_icon.png");
//infoImage.SetImageResource(resource);
//infoImag = customPin.Title;
infoTitle.Text = customPin.Title;
infoSummary.Text = customPin.MobileNo;
return view;
}
return null;
}
public Android.Views.View GetInfoWindow(Marker marker)
{
return null;
}
}
This code is in Xamarin.Forms project
public class CustomPin
{
public Pin Pin { get; set; }
public Guid Id { get; set; }
public string Title { get; set; }
public string MobileNo { get; set; }
public string Image { get; set; }
public string UserName { get; set; }
public UserType UserType { get; set; }
}
public class CustomMap : Map
{
public static readonly BindableProperty RouteCoordinatesProperty = BindableProperty.Create(nameof(RouteCoordinates), typeof(List<Position>), typeof(CustomMap), new List<Position>(), BindingMode.TwoWay);
public static readonly BindableProperty CustomPinsProperty = BindableProperty.Create(nameof(CustomPins), typeof(List<CustomPin>), typeof(CustomMap), new List<CustomPin>(), BindingMode.TwoWay);
public List<CustomPin> CustomPins
{
get { return (List<CustomPin>)GetValue(CustomPinsProperty); }
set { SetValue(CustomPinsProperty, value); }
}
public List<Position> RouteCoordinates
{
get { return (List<Position>)GetValue(RouteCoordinatesProperty); }
set { SetValue(RouteCoordinatesProperty, value); }
}
public Action<CustomPin> OnInfoWindowClicked;
public CustomMap()
{
RouteCoordinates = new List<Position>();
CustomPins = new List<CustomPin>();
}
}
This is how I use the custom map to render pin in Xamrin.Fomrs project
private void RenderPin(string longitudeCoordinate, string latitudeCoordinate,bool canMoveToLoacation, string lable,UserType userType,string mobileNo,string image,string userName)
{
double latitude = 0;
double longitude = 0;
double.TryParse(latitudeCoordinate, out latitude);
double.TryParse(longitudeCoordinate, out longitude);
var position = new Position(latitude, longitude);
var pin = new CustomPin
{
Pin = new Pin
{
Type = PinType.Place,
Position = position,
Label = lable,
},
Title = lable,
UserType = userType,
MobileNo = "Mobile No:" + mobileNo,
Image = "profile_images/" + image,
UserName = userName,
Id = Guid.NewGuid()
};
map.CustomPins.Add(pin);
map.Pins.Add(pin.Pin);
if (canMoveToLoacation)
{
map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(latitude, longitude), Distance.FromKilometers(2)));
}
}
If you install the Xamarin.Forms.Maps pre-release (2.3.5.255-pre5), you can now just override the CreateMarker() method in MapRenderer. It's much more elegant, and it fixes this problem with pins not being updated when added after map creation.
Has anyone integrated Google Place Autocomplete using Xamarin.Forms? I am to use it on a map for location suggestions. I've only seen resources for Xamarin.Android and Xamarin.iOS but on the part of implementing the AutoCompleteView, that I also don't know. I would be very thankful if someone could guide me with this. Thank you!
Place autocomplete can be implemented by using the Google Place API, whenever user enter a character, matching location with the entered character will be fetched from the Google server and binding back in User Interface.
Here's the link how to use Google Map Place API in Xamarin form:
http://www.appliedcodelog.com/2015/05/google-place-api-with-autocomplete-in.html
Also, please read the Official Google Places API Web Service:
https://developers.google.com/places/web-service/autocomplete
For iOS
[assembly: ExportRenderer(typeof(CustomAutoCompleteLocation), typeof(CustomAutoCompleteRenderer))]
namespace Aesthetic.iOS.Renderer
{
public class CustomAutoCompleteProfileRenderer : EntryRenderer
{
IntPtr inptr;
string tx = "1";
CustomAutoCompleteLocation _view;
Place place;
UITextField textView;
public UIWindow Window
{
get;
set;
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
_view = (CustomAutoCompleteLocation)Element;
if (Control != null)
{
textView = (UITextField)Control;
textView.Font = UIFont.FromName("Lato-Light", textView.Font.PointSize);
textView.BorderStyle = UITextBorderStyle.Line;
textView.Layer.BorderWidth = 1f;
textView.Layer.CornerRadius = 0f;
// do whatever you want to the textField here!
UIView paddingView = new UIView(new RectangleF(10, 16, 10, 16));
textView.LeftView = paddingView;
textView.LeftViewMode = UITextFieldViewMode.Always;
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var x = sender as CustomAutoComplete;
if (e.PropertyName == "IsFocused")
{
if (tx == "1")
{
Device.BeginInvokeOnMainThread(() =>
{
Window = new UIWindow(UIScreen.MainScreen.Bounds);
var controller = new LocationViewController();
Window.RootViewController = controller;
// make the window visible
Window.MakeKeyAndVisible();
controller.PlaceSelected1 += HandlePlaceSelection;
});
tx = "2";
}
else if (tx == "2") tx = "3";
else if (tx == "3") tx = "4";
else if (tx == "4") tx = "1";
}
}
private void HandlePlaceSelection(object sender, string locationData)
{
textView.Text = locationData;
Window.Hidden = true;
}
}
}
And this is location view controller
public partial class LocationViewController : UIViewController
{
public delegate void PlaceSelected(object sender, string locationData);
UITextField txtLocation;
public UIView backgroundView;
// UITextView txtLocation;
UIImageView googleAttribution;
UITableView tableViewLocationAutoComplete;
public event PlaceSelected PlaceSelected1;
public string strSampleString { get; set; }
LocationPredictionClass objAutoCompleteLocationClass;
string strAutoCompleteQuery;
LocationAutoCompleteTableSource objLocationAutoCompleteTableSource;
public LocationViewController() : base()
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
FnInitializeView();
FnClickEventInit();
}
void FnInitializeView()
{
backgroundView = new UIView(View.Frame);
backgroundView.BackgroundColor = UIColor.White;
View.AddSubview(backgroundView);
txtLocation = new UITextField();
txtLocation.Frame = new CoreGraphics.CGRect(20, 20, View.Frame.Width-20, 45.0f);
txtLocation.TranslatesAutoresizingMaskIntoConstraints = false;
txtLocation.ReturnKeyType = UIReturnKeyType.Done;
txtLocation.BackgroundColor = UIColor.FromRGB(221,221,221);
txtLocation.TextColor = UIColor.Black;
View.AddSubview(txtLocation);
txtLocation.BecomeFirstResponder();
tableViewLocationAutoComplete = new UITableView();
tableViewLocationAutoComplete.Frame = new CoreGraphics.CGRect(txtLocation.Frame.X, txtLocation.Frame.Height+ txtLocation.Frame.Y, View.Frame.Width, View.Frame.Height - txtLocation.Frame.Height - txtLocation.Frame.Y);
tableViewLocationAutoComplete.BackgroundColor = UIColor.White;
tableViewLocationAutoComplete.Source = objLocationAutoCompleteTableSource;
View.AddSubview(tableViewLocationAutoComplete);
tableViewLocationAutoComplete.Hidden = false;
txtLocation.ShouldReturn += (textField) => textField.ResignFirstResponder();
StringBuilder builderLocationAutoComplete = new StringBuilder(Constants.strPlacesAutofillUrl);
builderLocationAutoComplete.Append("?input={0}").Append("&key=").Append(Constants.strGooglePlaceAPILey);
strAutoCompleteQuery = builderLocationAutoComplete.ToString();
builderLocationAutoComplete.Clear();
builderLocationAutoComplete = null;
}
void FnClickEventInit()
{
txtLocation.EditingChanged += async delegate (object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtLocation.Text))
{
tableViewLocationAutoComplete.Hidden = true;
}
else
{
if (txtLocation.Text.Length > 2)
{
//Autofill
string strFullURL = string.Format(strAutoCompleteQuery, txtLocation.Text);
objAutoCompleteLocationClass = await RestRequestClass.LocationAutoComplete(strFullURL);
if (objAutoCompleteLocationClass != null && objAutoCompleteLocationClass.status == "OK")
{
if (objAutoCompleteLocationClass.predictions.Count > 0)
{
if (objLocationAutoCompleteTableSource != null)
{
objLocationAutoCompleteTableSource.LocationRowSelectedEventAction -= LocationSelectedFromAutoFill;
objLocationAutoCompleteTableSource = null;
}
tableViewLocationAutoComplete.Hidden = false;
objLocationAutoCompleteTableSource = new LocationAutoCompleteTableSource(objAutoCompleteLocationClass.predictions);
objLocationAutoCompleteTableSource.LocationRowSelectedEventAction += LocationSelectedFromAutoFill;
tableViewLocationAutoComplete.Source = objLocationAutoCompleteTableSource;
tableViewLocationAutoComplete.ReloadData();
}
else
tableViewLocationAutoComplete.Hidden = true;
}
else
{
tableViewLocationAutoComplete.Hidden = true;
}
}
}
};
}
void LocationSelectedFromAutoFill(Prediction objPrediction)
{
DismissViewController(true, null);
Console.WriteLine(objPrediction.description);
PlaceSelected1(this, objPrediction.description);
txtLocation.ResignFirstResponder();
}
}
public class RestRequestClass
{
static async Task<string> CallService(string strURL)
{
WebClient client = new WebClient();
string strResult;
try
{
strResult = await client.DownloadStringTaskAsync(new Uri(strURL));
}
catch
{
strResult = "Exception";
}
finally
{
client.Dispose();
client = null;
}
return strResult;
}
internal static async Task<LocationPredictionClass> LocationAutoComplete(string strFullURL)
{
LocationPredictionClass objLocationPredictClass = null;
LocationPredictionClass objLocationPredictClass12 = new LocationPredictionClass();
objLocationPredictClass12.predictions = new List<Prediction>();
string strResult = await CallService(strFullURL);
if (strResult != "Exception")
{
objLocationPredictClass = JsonConvert.DeserializeObject<LocationPredictionClass>(strResult);
}
foreach (Prediction objPred in objLocationPredictClass.predictions)
{
if (objPred.types[0] == "country")
{
objLocationPredictClass12.predictions.Add(objPred);
objLocationPredictClass12.status = "OK";
}
}
// string[] strPredictiveText = new string[data.Count];
//int index = 0;
// foreach (Prediction objPred in data)
// {
// strPredictiveText[index] = objPred.description;
// index++;
// }
return objLocationPredictClass12;
}
}
public class MatchedSubstring
{
public int length { get; set; }
public int offset { get; set; }
}
public class Term
{
public int offset { get; set; }
public string value { get; set; }
}
public class Prediction
{
public string description { get; set; }
public string id { get; set; }
public List<MatchedSubstring> matched_substrings { get; set; }
public string place_id { get; set; }
public string reference { get; set; }
public List<Term> terms { get; set; }
public List<string> types { get; set; }
}
public class LocationPredictionClass
{
public List<Prediction> predictions { get; set; }
public string status { get; set; }
}
public class Constants
{
public static string strGooglePlaceAPILey = "AIzaSyBXJntNIs2aAvKIRwrgCEwOGwnigbSWep8";
public static string strPlacesAutofillUrl = "https://maps.googleapis.com/maps/api/place/autocomplete/json";
}
public class LocationAutoCompleteTableSource : UITableViewSource
{
const string strCellIdentifier = "Cell";
readonly List<Prediction> lstLocations;
internal event Action<Prediction> LocationRowSelectedEventAction;
public LocationAutoCompleteTableSource(List<Prediction> arrItems)
{
lstLocations = arrItems;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return lstLocations.Count;
}
public override UIView GetViewForFooter(UITableView tableView, nint section)
{
return new UIView();
}
public override UITableViewCell GetCell(UITableView tableView, Foundation.NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell(strCellIdentifier) ?? new UITableViewCell(UITableViewCellStyle.Default, strCellIdentifier);
cell.TextLabel.Text = lstLocations[indexPath.Row].description;
cell.TextLabel.Font = UIFont.SystemFontOfSize(12);
return cell;
}
public override void RowSelected(UITableView tableView, Foundation.NSIndexPath indexPath)
{
if (LocationRowSelectedEventAction != null)
{
LocationRowSelectedEventAction(lstLocations[indexPath.Row]);
}
tableView.DeselectRow(indexPath, true);
}
}
}
I have a simple class
public class Colora
{
public Colora()
{
Created = DateTime.Now;
}
public string Name { get; set; }
public DateTime Created { get; set; }
}
that needs to stay simple (it is in a PCL). How do I turn this into a KVC compliant NSObject so that it can be displayed in a CollectionView?
I was thinking something like
List<Colora> coloras = new List<Colora> () {
new Colora() { Name = "Red" },
new Colora() { Name = "Blue" }
};
var objs = new NSObject[coloras.Count];
for (int i = 0; i < coloras.Count; i++) {
objs [i] = NSObject.FromObject (coloras [i]);
}
devCollectionView.Content = objs;
but FromObject does not work with my class. In this case the array does not have to be mutable.
Update. Remarkably, this works
public class ColoraViewModel : NSObject
{
private readonly Colora _colora;
public ColoraViewModel (Colora colora)
{
_colora = colora;
}
[Export("name")]
public string Name {
get { return _colora.Name; }
set {
if (_colora.Name != value) {
_colora.Name = value;
}
}
}
}
and
List<Colora> coloras = new List<Colora> () {
new Colora() { Name = "Red" },
new Colora() { Name = "Blue" }
};
var objs = new NSObject[coloras.Count];
for (int i = 0; i < coloras.Count; i++) {
objs [i] = new ColoraViewModel (coloras [i]);
}
devCollectionView.Content = objs;
though I'm still looking for a general way to create the KVC without having to make a new ViewModel.
This doesn't work, but I dream of something like this:
public class KVCObject : NSObject
{
private readonly object _t;
private readonly Type _type;
public KVCObject (object t)
{
_t = t;
_type = t.GetType();
}
public override void SetValueForKey (NSObject value, NSString key)
{
var info = _type.GetProperty (key.ToString ());
info.SetValue (_t, value);
}
public override NSObject ValueForKey (NSString key)
{
var info = _type.GetProperty (key.ToString ());
return NSObject.FromObject(info.GetValue (_t));
}
}
I am trying to return a dictionary from a WCF REST service as a JSON object. I want the format to be
{"key": "value"}
so I have created my own class as described here and here.
The service method works, sort of. The problem is that the key names are escaped. For example, if my dictionary contains "Hello world": 100, I get
{"Hello_x0020_world":100}. It also escapes other characters like %, etc.
Is there some way I can tell the serialization not to escape the names that way? It is almost like it is using xml rules that don't (necessarily) apply to JSON.
My serializable class:
[Serializable]
public class JsonDictionary : ISerializable
{
private Dictionary<string, object> _Dictionary;
public JsonDictionary()
{
_Dictionary = new Dictionary<string, object>();
}
public JsonDictionary(SerializationInfo info, StreamingContext context)
{
_Dictionary = new Dictionary<string, object>();
SerializationInfoEnumerator enumerator = info.GetEnumerator();
while (enumerator.MoveNext())
{
_Dictionary.Add(enumerator.Name, enumerator.Value);
}
}
public object this[string key]
{
get { return _Dictionary[key]; }
set { _Dictionary[key] = value; }
}
public void Add(string key, object value)
{
_Dictionary.Add(key, value);
}
public bool ContainsKey(string key)
{
return _Dictionary.ContainsKey(key);
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
foreach (string key in _Dictionary.Keys)
info.AddValue(key, _Dictionary[key], _Dictionary[key] == null ? typeof(object) : _Dictionary[key].GetType());
}
}
My service function definition:
[WebGet(UriTemplate = "", ResponseFormat = WebMessageFormat.Json)]
public JsonDictionary GetCollection()
{
JsonDictionary dict = new JsonDictionary();
dict.Add("Hello world", 100);
return dict;
}
The DataContractJsonSerializer used by default in WCF uses a XML-to-JSON mapping which causes some issues, like the one you see in ISerializable types. You can, however, use a custom formatter to change how the response will be serialized. In the example below, I'm using JSON.NET which deals with ISerializable objects "correctly".
public class StackOverflow_16674152
{
[Serializable]
public class JsonDictionary : ISerializable
{
private Dictionary<string, object> _Dictionary;
public JsonDictionary()
{
_Dictionary = new Dictionary<string, object>();
}
public JsonDictionary(SerializationInfo info, StreamingContext context)
{
_Dictionary = new Dictionary<string, object>();
SerializationInfoEnumerator enumerator = info.GetEnumerator();
while (enumerator.MoveNext())
{
_Dictionary.Add(enumerator.Name, enumerator.Value);
}
}
public object this[string key]
{
get { return _Dictionary[key]; }
set { _Dictionary[key] = value; }
}
public void Add(string key, object value)
{
_Dictionary.Add(key, value);
}
public bool ContainsKey(string key)
{
return _Dictionary.ContainsKey(key);
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
foreach (string key in _Dictionary.Keys)
info.AddValue(key, _Dictionary[key], _Dictionary[key] == null ? typeof(object) : _Dictionary[key].GetType());
}
}
[ServiceContract]
public class Service
{
[WebGet(UriTemplate = "", ResponseFormat = WebMessageFormat.Json)]
[MyISerializableResponseJsonBehavior]
public JsonDictionary GetCollection()
{
JsonDictionary dict = new JsonDictionary();
dict.Add("Hello world", 100);
return dict;
}
}
public class MyFormatter : IDispatchMessageFormatter
{
IDispatchMessageFormatter original;
string replyAction;
public MyFormatter(IDispatchMessageFormatter original, string replyAction)
{
this.original = original;
this.replyAction = replyAction;
}
public void DeserializeRequest(Message message, object[] parameters)
{
this.original.DeserializeRequest(message, parameters);
}
public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
{
ISerializable serializable = result as ISerializable;
if (serializable != null)
{
string json = JsonConvert.SerializeObject(serializable);
byte[] bytes = Encoding.UTF8.GetBytes(json);
var writer = new MyRawWriter(bytes);
Message reply = Message.CreateMessage(messageVersion, replyAction, writer);
reply.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Raw));
return reply;
}
else
{
return this.original.SerializeReply(messageVersion, parameters, result);
}
}
class MyRawWriter : BodyWriter
{
byte[] data;
public MyRawWriter(byte[] data)
: base(true)
{
this.data = data;
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
writer.WriteStartElement("Binary");
writer.WriteBase64(data, 0, data.Length);
writer.WriteEndElement();
}
}
}
public class MyISerializableResponseJsonBehaviorAttribute : Attribute, IOperationBehavior
{
public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
if (operationDescription.Messages.Count > 1)
{
dispatchOperation.Formatter = new MyFormatter(dispatchOperation.Formatter, operationDescription.Messages[1].Action);
}
}
public void Validate(OperationDescription operationDescription)
{
if (operationDescription.Messages.Count > 1)
{
var respMessage = operationDescription.Messages[1];
if (respMessage.Body.Parts.Count > 0)
{
throw new InvalidOperationException("Cannot be used with out/ref parameters");
}
}
var wga = operationDescription.Behaviors.Find<WebGetAttribute>();
var wia = operationDescription.Behaviors.Find<WebInvokeAttribute>();
WebMessageBodyStyle bodyStyle = WebMessageBodyStyle.Bare; // default
if (wga != null && wga.IsBodyStyleSetExplicitly) {
bodyStyle = wga.BodyStyle;
}
if (wia != null && wia.IsBodyStyleSetExplicitly) {
bodyStyle = wia.BodyStyle;
}
if (bodyStyle == WebMessageBodyStyle.Wrapped || bodyStyle == WebMessageBodyStyle.WrappedResponse)
{
throw new InvalidOperationException("This behavior can only be used with bare response style");
}
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(Service), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
host.Open();
Console.WriteLine("Host opened");
WebClient c = new WebClient();
Console.WriteLine(c.DownloadString(baseAddress + "/"));
}
}