New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unsupported jsonb number 123 #1788

Closed
rrichardson opened this Issue Jul 18, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@rrichardson

rrichardson commented Jul 18, 2018

Setup

osx brew install postgres

$ otool -L target/debug/fries
target/debug/fries:
/usr/local/opt/postgresql/lib/libpq.5.dylib (compatibility version 5.0.0, current version 5.10.0)

[dependencies]
diesel = { version = "1.0.0", features = ["postgres", "serde_json"] }

Versions

  • Rust: .rustc 1.27.1 (5f2b325f6 2018-07-07)
  • Diesel: 1.0.0
  • Database: . Postgresql 9.6 or 10, libpq 5.10
  • Operating System . OSX High Sierra

Feature Flags

  • diesel: . postgres . serde_json

Problem Description

When running my importer app, it fails to insert into a Jsonb field and errors with:

DatabaseError(__Unknown, "unsupported jsonb version number 123")

I reckon it is related to lib/pq#528
This makes me think that maybe serializing the json into a byte array is the wrong thing.. but I'm not sure what the correct approach is.. (see fn to_sql below)

What are you trying to accomplish?

Insert my custom serde_json type into a jsonb column

What is the expected output?

Success

What is the actual output?

DatabaseError(__Unknown, "unsupported jsonb version number 123")

Are you seeing any additional errors?

None that I can find

Steps to reproduce

#[serde(rename_all = "kebab-case")]
#[derive(Serialize, Deserialize, Debug, PartialEq, FromSqlRow, AsExpression)]
#[sql_type="Jsonb"]
pub struct Pos {
    object_type: String,
    reference: String,
    offset: i32,
    is_closed: Option<bool>,
}

impl ToSql<Jsonb, Pg> for Pos {
    fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
        let j = serde_json::to_string(self)?;
        out.write_all(j.as_bytes())?;
        Ok(IsNull::No)
    }
}

impl FromSql<Jsonb, Pg> for Pos {
    fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
        let p : Pos = serde_json::from_slice(not_none!(bytes))?;
        Ok(p)
    }
}

#[serde(rename_all = "kebab-case")]
#[derive(Insertable, Queryable, Deserialize)]
#[table_name="sentences"]
pub struct Sentence {
  start_pos: Pos,
  end_pos: Pos,
}

table! {
    sentences (id) {
        start_pos -> Nullable<Jsonb>,
        end_pos -> Nullable<Jsonb>,
    }
}

Checklist

  • [x ] I have already looked over the issue tracker for similar issues.
  • [x ] This issue can be reproduced on Rust's stable channel. (Your issue will be
    closed if this is not the case)
@rrichardson

This comment has been minimized.

rrichardson commented Jul 18, 2018

Found the problem. Since I'm providing the binary format, I need to prefix the value with 1 as is done in the ToSql impl for serde_json::Value.

@sgrif

This comment has been minimized.

Member

sgrif commented Jul 18, 2018

You should avoid relying on the concrete representation of PG's json types or Diesel specific implementations. Instead you should work in terms of serde_json::Value types, which are already supported, and then call that types ToSql/FromSql implementations.

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