https://github.com/catcherwong/nancy.response.messagepack
MessagePack Response for Nancy.
https://github.com/catcherwong/nancy.response.messagepack
messagepack msgpack nancy response
Last synced: 8 months ago
JSON representation
MessagePack Response for Nancy.
- Host: GitHub
- URL: https://github.com/catcherwong/nancy.response.messagepack
- Owner: catcherwong
- License: mit
- Created: 2018-01-06T12:26:52.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-09T15:20:13.000Z (over 8 years ago)
- Last Synced: 2025-08-01T08:02:17.626Z (10 months ago)
- Topics: messagepack, msgpack, nancy, response
- Language: C#
- Homepage:
- Size: 10.7 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Nancy.Responses.MessagePack
There are many response type we can choose when we use Nancy to build our Web API . For example , JSON , XML , ProtBuf . etc .
JSON is the most popular , but there are a few pros in replacing the default JSON serializator in Nancy with MessagePack:
- JSON uses 4 bytes to represent null, MessagePack only requires 1 byte;
- JSON uses 2 bytes to represent a typical int, MessagePack requires 1 byte and so on;
- MessagePack is faster to read and write than JSON.
ProtBuf is also a good choose , but when we use ProtBuf , we need to mark our class as `[ProtoContract]` and public members(property or field) mark as `[ProtoMember]`. This is a little complex!
So `Nancy.Responses.MessagePack` is come ! `Nancy.Responses.MessagePack` use [MessagePack-CSharp](https://github.com/neuecc/MessagePack-CSharp) as the default Serializer.
## Quick Start
### Instatll the package at first
```
Install-Package Nancy.Response.MessagePack
```
### Define A Class
```csharp
public class User
{
public string Name { get; set; }
public int Age { get; set; }
}
```
### Specify MessagePack Response
```csharp
using Nancy.Responses;
public class MainModule : NancyModule
{
public MainModule()
{
Get("/{name}/{age}", _ =>
{
var data = new User { Name = args.name, Age = args.age };
return Response.AsMessagePack(data);
});
Get("/other", _ =>
{
return Response.AsMessagePack("your value");
});
}
}
```
### Based On User's Request
```csharp
public class MainModule : NancyModule
{
public MainModule()
{
Get("/{name}/{age}", args =>
{
var data = new User { Name = args.name, Age = args.age };
return Negotiate.WithModel(data);
});
}
}
```