Skip to content

JSON Support #168

Closed
Closed

Description

@drusellers

I am currently trying to get NHibernate to work with JSON data type. I am currently getting stuck on ERROR: 42804: column "post" is of type json but expression is of type text - I have used Npgsql directly and it works just fine. I am thinking that step one might be to add Json to NpgsqlDbType but after that I am not 100% sure where to go.

Activity

  1. drusellers commented on Feb 26, 2014

    @drusellers
    Author

    Ok, I traced it down to 'UseCast'. NHibernate is setting the NpgsqlParameter in some fashion that is turning UseCast = true and is setting it to text (as it has been instructed to do). What I would love here is a way to tell it that NpgsqlDbType is Json and I wonder if that wouldn't fix my problems right up.

  2. drusellers commented on Feb 26, 2014

    @drusellers
    Author

    I have worked around the issue with the following horrible code.

    var parameter = cmd.Parameters[index];
    parameter.Value = value;
    
    var field = typeof (NpgsqlParameter).GetField("useCast", BindingFlags.NonPublic | BindingFlags.Instance);
    field.SetValue(parameter, false);
    
  3. franciscojunior commented on Feb 26, 2014

    @franciscojunior
    Member

    Hi, @drusellers !

    Indeed, Npgsql needs a better way to handle type casting. I even created an issue about that: #125 .

    In order to add json datatype support, besides adding NpgsqlDbType, you will need to add a line to PrepareDefaultTypesMap method in NpgsqlTypesHelper:

    nativeTypeMapping.AddType("json", NpgsqlDbType.Json, DbType.Object, true);
    

    If you want to provide a custom converter in text format and/or binary format, you can do so by specifying your custom converters when calling AddType. You may have a look at current converters like:

    nativeTypeMapping.AddType("float4", NpgsqlDbType.Real, DbType.Single, false,
                                                BasicNativeToBackendTypeConverter.SingleDoubleToFloat4Float8Text,
                                                BasicNativeToBackendTypeConverter.SingleToFloat4Binary);
    

    We can work together on this issue to add it.

  4. drusellers commented on Mar 2, 2014

    @drusellers
    Author

    I would definitely appreciate any help you can provide on the topic.

  5. franciscojunior commented on Mar 5, 2014

    @franciscojunior
    Member

    Sure! I'll play with it and will let you know what I get.

    I'll use this issue to create documentation about how to add custom types support to Npgsql. I think this will help a lot of developers who work with Npgsql. :)

  6. drusellers commented on Mar 29, 2014

    @drusellers
    Author

    @franciscojunior what's the best way for me to help?

  7. franciscojunior commented on Mar 29, 2014

    @franciscojunior
    Member

    Hi, @drusellers ! I'm really sorry I didn't check this issue yet. :(
    I was working with some other issues related to Npgsql releases.

    As things seem to be going ok now, I can work on this issue too. Give me some time while I write some documentation about how to add a custom type. I'll use json type as the example.

    Sorry for making you wait :(

  8. drusellers commented on Mar 30, 2014

    @drusellers
    Author

    No worries. I have a work around. Just not sure how best to help. -d

    On Sat, Mar 29, 2014 at 5:29 PM, Francisco Figueiredo Jr.
    [email protected] wrote:

    Hi, @drusellers ! I'm really sorry I didn't check this issue yet. :(
    I was working with some other issues related to Npgsql releases.
    As things seem to be going ok now, I can work on this issue too. Give me some time while I write some documentation about how to add a custom type. I'll use json type as the example.

    Sorry for making you wait :(

    Reply to this email directly or view it on GitHub:
    #168 (comment)

  9. franciscojunior commented on Mar 30, 2014

    @franciscojunior
    Member

    Great! I'm glad to know I'm not giving you too much trouble :) Thanks for your comprehension.

  10. added this to the milestone on Apr 8, 2014
  11. squidge commented on Apr 15, 2014

    @squidge

    Hello,

    I have been getting the same issue "ERROR: 42804: column "x" is of type json but expression is of type text"

    Is this a Npgsql issue or OrmLite which is what I am using in this case.

    Many thanks

  12. drusellers commented on Apr 15, 2014

    @drusellers
    Author

    I believe it to ultimately be a 'Npgsql' issue - because its trying to do a cast in the sql to 'text' when the column is 'json' - in order to get past it I had to do the following 'hack' to make it work, for more details see above.

    var parameter = cmd.Parameters[index];
    parameter.Value = value;
    
    var field = typeof (NpgsqlParameter).GetField("useCast", BindingFlags.NonPublic | BindingFlags.Instance);
    field.SetValue(parameter, false);
    
  13. squidge commented on Apr 15, 2014

    @squidge

    I see, I'll try your hack then while a fix is put in place..

    Many thanks

  14. squidge commented on Apr 16, 2014

    @squidge

    @drusellers so where exactly did you put this code?

  15. drusellers commented on Apr 16, 2014

    @drusellers
    Author

    I don't know OrmLite enough to say where - I did mine with NHibernate and put it in an IUserType - but the gist is you'll need to get access to the NpgsqlParameter that is handling the JSON column type.

  16. squidge commented on Apr 17, 2014

    @squidge

    I found the place where to put it in OrmLite and it works. I also found out you can do the same in Npgsql and "turn off" casting but the hack on the ORM is a better place I would think.

    Thanks for your help

  17. franciscojunior commented on Apr 17, 2014

    @franciscojunior
    Member

    Hi, all!
    I'm working to add support for json datatype. I'll also use this opportunity to add documentation to Npgsql about how to add datatypes so others can be easily added in the future.

    Sorry for taking so long. I hope to get something about it by the weekend.

  18. franciscojunior commented on Apr 17, 2014

    @franciscojunior
    Member

    Just created a pull request which adds support for json datatype.

    @drusellers , you will now be able to set the NpgsqlDbType to Json.

    For while, the only way to get json support is to set the NpgsqlDbType. As json datatype looks just like string, Npgsql won't be able to tell the difference between a text and json if you simply set the parameter value.

    Regarding your changes to omit the parameter cast (which is also another possible solution) I'm working on this support too. See #146 and #125

  19. franciscojunior commented on Apr 17, 2014

    @franciscojunior
    Member

    I also added a wiki page which explains how to add custom datatypes to Npgsql. It is very basic at this moment, but I'll fill it with more details. Please, check it out and let me know what you think.
    https://github.com/npgsql/Npgsql/wiki/How-to-add-a-custom-type-to-Npgsql

  20. drusellers commented on Apr 17, 2014

    @drusellers
    Author

    👍 AWESOME

    On Thu, Apr 17, 2014 at 4:14 PM, Francisco Figueiredo Jr. <
    [email protected]> wrote:

    I also added a wiki page which explains how to add custom datatypes to
    Npgsql. It is very basic at this moment, but I'll fill it with more
    details. Please, check it out and let me know what you think.
    https://github.com/npgsql/Npgsql/wiki/How-to-add-a-custom-type-to-Npgsql

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/168#issuecomment-40763712
    .

  21. franciscojunior commented on Jun 27, 2014

    @franciscojunior
    Member

    @drusellers , we added support for json and jsonb and hstore as well. See #242 (comment) for more information.

    Would you mind to check latest master branch and test if it is working for you now?

    Thanks in advance.

  22. roji commented on Jul 17, 2014

    @roji
    Member

    I'm going to go ahead and close this, if any issue is found with JSON support please reopen.

  23. drusellers commented on Nov 7, 2014

    @drusellers
    Author

    👍 everything looks good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions