Using the WCF DataContractJsonSerializer in .NET 3.5
Asked Answered
C

1

2

I'm trying to use the geocoding code from here in my ASP.NET MVC 2 site. Unfortunately, some of that code, specifically the DataContractJsonSerializer usage, is only possible through .NET 4.0. As my hosting provider doesn't support .NET 4, I'm forced to implement that functionality in .NET 3.5.

How can I rework the code (which I have reposted below) to work in .NET 3.5?

The Google Maps Geocoding API can also return XML, if that's easier to serialize in 3.5...


Below is the code I'm trying to convert from .NET 4 to .NET 3.5:

using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Net;
using System.Web;

.
.
.

private static GeoResponse CallGeoWS(string address)
{
        string url = string.Format(
                "http://maps.google.com/maps/api/geocode/json?address={0}&region=dk&sensor=false",
                HttpUtility.UrlEncode(address)
                );
        var request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
        request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GeoResponse));
        var res = (GeoResponse)serializer.ReadObject(request.GetResponse().GetResponseStream());
        return res;
}

[DataContract]
class GeoResponse
{
        [DataMember(Name="status")]
        public string Status { get; set; }
        [DataMember(Name="results")]
        public CResult[] Results { get; set; }

        [DataContract]
        public class CResult
        {
                [DataMember(Name="geometry")]
                public CGeometry Geometry { get; set; }

                [DataContract]
                public class CGeometry
                {
                        [DataMember(Name="location")]
                        public CLocation Location { get; set; }

                        [DataContract]
                        public class CLocation
                        {
                                [DataMember(Name="lat")]
                                public double Lat { get; set; }
                                [DataMember(Name = "lng")]
                                public double Lng { get; set; }
                        }
                }
        }
}
Connell answered 5/7, 2010 at 9:0 Comment(0)
C
16

What is the specific problem that you're hitting?

Without more details it's difficult to diagnose the exact problem, but DataContractJsonSerializer is available in .NET 3.5 - you'll need to manually add a reference to System.ServiceModel.Web.dll.

(Note that the MSDN documentation misleadingly states that DataContractJsonSerializer can be found in System.Runtime.Serialization.dll. While this is true for .NET 4, the .NET 3.5 version of DataContractJsonSerializer actually lives in System.ServiceModel.Web.dll.)

Clash answered 5/7, 2010 at 9:7 Comment(7)
Weird... I did add a reference to version 3.0 of that DLL, but it tells me it still can't find the type - and as for using statements, I have one that references System.Runtime.Serialization. What gives?Connell
@Maxim: Try using System.Runtime.Serialization.Json;Clash
@Clash when I do that it tells me that it "can't find that namespace or type." I'm pretty sure it's something with my assembly references; is v3 the right version of the DLL?Connell
@Maxim: Apologies. The MSDN docs for .NET 3.5 incorrectly state that DataContractJsonSerializer lives in System.Runtime.Serialization.dll. This is true in .NET 4, but in 3.5 it's found in System.ServiceModel.Web.dll. (I'll update my answer accordingly.)Clash
Thanks @Clash I was looking for the dll too, good to know that it's in System.ServiceModel.Web.dllGirardo
well it is available in both System.Runtime.Serialization.dll as well as in System.ServiceModel.Web.dll. [link] (forums.silverlight.net/post/47045.aspx)Prosecutor
Instead of using System.Runtime.Serialization.dll/ System.ServiceModel.Web.dll switch to NewtonSoft.Json libraryCoverdale

© 2022 - 2024 — McMap. All rights reserved.