Coder Social home page Coder Social logo

Comments (5)

eoin55 avatar eoin55 commented on June 1, 2024

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.

eoin55 avatar eoin55 commented on June 1, 2024

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.

christianz avatar christianz commented on June 1, 2024

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.

eoin55 avatar eoin55 commented on June 1, 2024

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.

christianz avatar christianz commented on June 1, 2024

Excellent! I see you've also updated the nuget package. This makes my life a whole lot easier :-)

from honeybear.halclient.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.