Comments (5)
Hi @christianz,
Thanks for getting in touch. It's nice to hear from someone using it in anger :)
I'll take a look at this issue over the weekend. It shouldn't be a problem to fix it.
If you have any other issues or suggestions, just let me know.
Cheers,
Eoin
from honeybear.halclient.
Hi @christianz,
I've added support for application/hal+json. I tested on my own sample API and it seems to work fine. This is available from v1.0.29.
I didn't add the ability to specify the MediaTypeFormatter as a constructor parameter as I can't think of any other content types that might need to be supported.
If you have any other issues or suggestions, just let me know. All feedback is welcome :)
Cheers,
Eoin
from honeybear.halclient.
Hi!
Great work, thank you.
The other reason I want to specify my own MediaTypeFormatter is because the API I'm consuming doesn't accept dates in the format serialized by the default formatter. I'd also like to convert property names to lower camel case on serialization, but that's a minor issue (I can change my property names manually if need be).
The date thing might be a weakness in the API I'm using, but it would still be nice to circumvent this weakness by just passing in a custom MediaTypeFormatter.
This is the code I have currently:
public class HalJsonMediaTypeFormatter : JsonMediaTypeFormatter
{
public HalJsonMediaTypeFormatter()
{
var serializerSettings = new JsonSerializerSettings();
serializerSettings.ContractResolver = new LowerCamelCaseContractResolver();
serializerSettings.NullValueHandling = NullValueHandling.Ignore;
serializerSettings.Converters.Add(new DumbDateTimeConverter());
SerializerSettings = serializerSettings;
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/hal+json"));
}
}
/// <summary>
/// Converts property names to lower camel case.
/// </summary>
public class LowerCamelCaseContractResolver : DefaultContractResolver
{
protected override string ResolvePropertyName(string propertyName)
{
var propName = propertyName.ToCharArray();
var firstChar = char.ToLower(propName[0]);
return firstChar + propertyName.Substring(1);
}
}
class DumbDateTimeConverter : DateTimeConverterBase
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var dtVal = (DateTime)value;
writer.WriteValue(dtVal.ToString("yyyy-MM-dd"));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return DateTime.ParseExact(existingValue.ToString(), "yyyy-MM-dd", null);
}
}
What do you think?
Christian
from honeybear.halclient.
Hi @christianz,
Yep, I think that's a valid usage. I hadn't considered it.
I've overloaded the HalClient constructor, so that now you can pass in a list of MediaTypeFormatters. By default it will use the HalJsonMediaTypeFormatter.
This is available in v1.0.34.
Let me know how you get on.
Cheers,
Eoin
from honeybear.halclient.
Excellent! I see you've also updated the nuget package. This makes my life a whole lot easier :-)
from honeybear.halclient.
Related Issues (11)
- How do I access nested Links? HOT 7
- Async support HOT 5
- Add support for HAL links retrieval HOT 3
- Fill DTO Object with Elements from the resource and embedded Objects? HOT 4
- Retrieve single objects HOT 2
- Problem getting empty list from _embedded
- Be able to specify which MediaTypeFormatter to use when PUT or POSTing HOT 1
- Support for .NET 4.5 HOT 2
- NUnit 2.6.4 in VS2015 HOT 8
- HAL Forms HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from honeybear.halclient.