Can the ServiceStack JsonSerializer be customized to support a custom GeoJSON format

935 views
Skip to first unread message

Vish

unread,
Jul 13, 2011, 7:59:18 AM7/13/11
to ServiceStack .NET Open Source REST Web Services Framework
Hi all,

I have been reviewing ServiceStack alongside the Web API from MS for
use in our projects. One of the things I need to be able to do is to
output JSON for spatial data in a custom format defined by a spec here
http://geojson.org/geojson-spec.html

Is this possible with the ServiceStack JsonSerializer? Can the output
from it be customized? I would appreciate it if someone could link to
me any docs on it?

Thank You,
Vish

Demis Bellot

unread,
Jul 13, 2011, 1:22:12 PM7/13/11
to servic...@googlegroups.com
Hi Vish,

Yeah, that looks like a fairly standard JSON format which you should be able to accomplish with Custom POCO types.
Note: You can also Dictionary<string,string> and anonymous types to generate custom JSON objects.

I've just created a documentation page for the JSON Serializer viewable at:

I'll look at expanding on that a little if I run into some spare time tomorrow.

Cheers,

Vish

unread,
Jul 13, 2011, 1:55:30 PM7/13/11
to ServiceStack .NET Open Source REST Web Services Framework
So, it seems like I can't customize the serialization process... Am I
right on that one?

Also, I can't seem to be able to view the sample link you have in that
post.

Will I be able to serialize the point class below

class point {
public string X{get;set;}
public string Y{get;set;}
}

to "[10, 10]" if the object had X = 10 and Y = 10

So, that my line DTO can look like

class line {
List<point> points{get;set;}
}


The above DTOs are much more usable and intuitive than representing
point as an int[] etc...

Also, can i use service stack with .Net 4.0 framework? If so, will it
handle tuple serialization properly?

Thank You,
Vish





On Jul 13, 1:22 pm, Demis Bellot <demis.bel...@gmail.com> wrote:
> Hi Vish,
>
> Yeah, that looks like a fairly standard JSON format which you should be able
> to accomplish with Custom POCO types.
> Note: You can also Dictionary<string,string> and anonymous types to generate
> custom JSON objects.
>
> I've just created a documentation page for the JSON Serializer viewable at:http://www.servicestack.net/docs/text-serializers/json-serializer
>
> I'll look at expanding on that a little if I run into some spare time
> tomorrow.
>
> Cheers,
>

Demis Bellot

unread,
Jul 14, 2011, 1:12:24 PM7/14/11
to servic...@googlegroups.com
Well although the customizability options are limited at the moment, there are a few options. 
If you could return a string value you can use struct's to completely customize a types JSON output, e.g:


public struct PointStruct
{
public double X { get; set; }
public double Y { get; set; }
public override string ToString()
{
return "(" + X + "," + Y + ")";
}
public static PointStruct Parse(string json)
{
var points = json.FromJson<int[]>();
return new PointStruct { X = points[0], Y = points[1] };
}
}

[Test]
public void Can_serialize_GeoJSON_point_with_structs()
{
Assert.That(new PointStruct{ X = 1, Y = 2}.ToJson(), Is.EqualTo("\"(1,2)\""));
}

Although since this needs to be in quotes then it may not be the best option.

Though in this case, for the customizable array output as above you can still use a custom class if it's IEnumerable since it will be treated and serialized to a JSON array, e.g:

public class PointArray : IEnumerable
{
double[] points = new double[2];
public double X 
{
get
{
return points[0];
}
set 
{
points[0] = value;
}
}
public double Y
{
get
{
return points[1];
}
set 
{
points[1] = value;
}
}
public IEnumerator GetEnumerator()
{
foreach (var point in points) 
yield return point;
}
}

[Test]
public void Can_serialize_GeoJSON_point_with_PointArray()
{
Assert.That(new PointArray{ X = 1, Y = 2 }.ToJson(), Is.EqualTo("[1,2]"));

Vish

unread,
Jul 14, 2011, 7:40:04 PM7/14/11
to ServiceStack .NET Open Source REST Web Services Framework
Great, The IEnumerable solution will work like a charm. Amazing. Still
dumbfounded by servicestack's capabilities. Who needs WCF? :)

Did ServiceStack grow organically from various different projects or
was it just created with this vision in mind from the get go?

Thank You,
Vish

Demis Bellot

unread,
Jul 14, 2011, 9:44:55 PM7/14/11
to servic...@googlegroups.com
It pretty much grew organically out of necessity and have over time just kept adding features as I or others have needed them.

I've worked in a few places where I've needed to rapidly develop web services and going the WCF route had caused too much friction and intertia in the past especially when trying to maintain and develop multiple SOA services at once. 

IMHO the whole concept behind WCF is fundamentally wrong and in need of a rewrite. Trying to abstract all network endpoints just makes everything dumb where you're discouraged to use HTTP specific features and makes it harder to do anything by design not to mention the configuration hell that ensues. 

So for the last few years I happily haven't needed to use WCF since and don't expect to ever do so again. 

Cheers,
Reply all
Reply to author
Forward
0 new messages