<commit_before>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta id="layout-head" />
    <title>avaje ebean</title>
    <link rel="shortcut icon" href="/images/favicon.ico" >
    <!--<link href="/css/bootstrap.min.css" rel="stylesheet">-->
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/pygments.css" type="text/css" />
    <link rel="stylesheet" href="/css/site.css">
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css">
      <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
  <body>
  <div id="wrap">
    <div id="header">
    <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="/">Ebean ORM</a>
        </div>
        <div class="collapse navbar-collapse">
          <ul class="nav navbar-nav pull-right">
            <li ><a href="/"><span class="glyphicon glyphicon-home"></span>  Home</a></li>
            <li ><a href="/videos">Videos</a></li>
            <li ><a href="/quickstart">Quick Start</a></li>
            <li ><a href="/support">Support</a></li>
            <li class="active"><a href="/docs">Docs</a></li>
            <li ><a target="_blank" href="/apidocs">ApiDocs</a></li>
            <li ><a href="/releases">Releases</a></li>
            <li ><a target="_blank" href="https://github.com/ebean-orm/avaje-ebeanorm"> <i class="fa fa-github"></i> Github</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </div>

  </div>
  <div id="main" data-offset="60">
<div class="jumbotron mini">
  <div class="container">
    <h1><a href="/docs">Documentation</a> / JSON </h1>
  </div>
</div>

<div class="container doc-index bs-docs-container">

  <div class="row">

    <div class="col-md-9" role="main">

      <div class="bs-docs-section">
<h1 id="json">JSON</h1>
...(under construction)...

<p>

Ebean can hydrate a bean from json.  It will ignore the json properties that do not match up to properties on the bean.

<div class="syntax java"><div class="highlight"><pre><span class="n">String</span> <span class="n">jsonWithUnknown</span> <span class="o">=</span> <span class="s">&quot;{\&quot;id\&quot;:42, \&quot;unknownProp\&quot;:\&quot;foo\&quot;, \&quot;name\&quot;:\&quot;rob\&quot;, \&quot;version\&quot;:1}&quot;</span><span class="o">;</span>

<span class="n">Customer</span> <span class="n">customer</span> <span class="o">=</span> <span class="n">Ebean</span><span class="o">.</span><span class="na">json</span><span class="o">().</span><span class="na">toBean</span><span class="o">(</span><span class="n">Customer</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">jsonWithUnknown</span><span class="o">);</span>
<span class="n">assertEquals</span><span class="o">(</span><span class="n">Integer</span><span class="o">.</span><span class="na">valueOf</span><span class="o">(</span><span class="mi">42</span><span class="o">),</span> <span class="n">customer</span><span class="o">.</span><span class="na">getId</span><span class="o">());</span>
<span class="n">assertEquals</span><span class="o">(</span><span class="s">&quot;rob&quot;</span><span class="o">,</span> <span class="n">customer</span><span class="o">.</span><span class="na">getName</span><span class="o">());</span>
</pre></div>
</div>

</p>
</div>

    </div>

    <div class="col-md-3" role="complementary">
      <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm">
        <ul class="nav bs-docs-sidenav">
<li >
  <a href="/docs/introduction">Introduction</a>
</li><commit_msg>docs / JSON jsonb<commit_after>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta id="layout-head" />
    <title>avaje ebean</title>
    <link rel="shortcut icon" href="/images/favicon.ico" >
    <!--<link href="/css/bootstrap.min.css" rel="stylesheet">-->
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/pygments.css" type="text/css" />
    <link rel="stylesheet" href="/css/site.css">
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css">
      <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
  <body>
  <div id="wrap">
    <div id="header">
    <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="/">Ebean ORM</a>
        </div>
        <div class="collapse navbar-collapse">
          <ul class="nav navbar-nav pull-right">
            <li ><a href="/"><span class="glyphicon glyphicon-home"></span>  Home</a></li>
            <li ><a href="/videos">Videos</a></li>
            <li ><a href="/quickstart">Quick Start</a></li>
            <li ><a href="/support">Support</a></li>
            <li class="active"><a href="/docs">Docs</a></li>
            <li ><a target="_blank" href="/apidocs">ApiDocs</a></li>
            <li ><a href="/releases">Releases</a></li>
            <li ><a target="_blank" href="https://github.com/ebean-orm/avaje-ebeanorm"> <i class="fa fa-github"></i> Github</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </div>

  </div>
  <div id="main" data-offset="60">
<div class="jumbotron mini">
  <div class="container">
    <h1><a href="/docs">Documentation</a> / JSON </h1>
  </div>
</div>

<div class="container doc-index bs-docs-container">

  <div class="row">

    <div class="col-md-9" role="main">

      <div class="bs-docs-section">
<h1 id="json">JSON</h1>
...(under construction)...

<p>

Ebean can hydrate a bean from json.  It will ignore the json properties that do not match up to properties on the bean.

<div class="syntax java"><div class="highlight"><pre><span class="n">String</span> <span class="n">jsonWithUnknown</span> <span class="o">=</span> <span class="s">&quot;{\&quot;id\&quot;:42, \&quot;unknownProp\&quot;:\&quot;foo\&quot;, \&quot;name\&quot;:\&quot;rob\&quot;, \&quot;version\&quot;:1}&quot;</span><span class="o">;</span>

<span class="n">Customer</span> <span class="n">customer</span> <span class="o">=</span> <span class="n">Ebean</span><span class="o">.</span><span class="na">json</span><span class="o">().</span><span class="na">toBean</span><span class="o">(</span><span class="n">Customer</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">jsonWithUnknown</span><span class="o">);</span>
<span class="n">assertEquals</span><span class="o">(</span><span class="n">Integer</span><span class="o">.</span><span class="na">valueOf</span><span class="o">(</span><span class="mi">42</span><span class="o">),</span> <span class="n">customer</span><span class="o">.</span><span class="na">getId</span><span class="o">());</span>
<span class="n">assertEquals</span><span class="o">(</span><span class="s">&quot;rob&quot;</span><span class="o">,</span> <span class="n">customer</span><span class="o">.</span><span class="na">getName</span><span class="o">());</span>
</pre></div>
</div>

  <h2>Postgres JSONB</h2>
  <p>

  </p>

  <h3>@DbJsonB</h3>
  <p>
    In your entity bean you can annotate a property with <code>@DbJsonB</code> and that indicates that the property
    maps to a JSONB database type.  For non-Postgres databases this maps to a database Clob.
  </p>
<div class="syntax java"><div class="highlight"><pre><span class="nd">@Entity</span>
<span class="nd">@Table</span><span class="o">(</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;p_doc&quot;</span><span class="o">)</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">SimpleDoc</span> <span class="kd">extends</span> <span class="n">Model</span> <span class="o">{</span>

  <span class="nd">@Id</span>
  <span class="n">Long</span> <span class="n">id</span><span class="o">;</span>
  <span class="o">...</span>

  <span class="nd">@DbJsonB</span>
  <span class="n">Map</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span><span class="n">Object</span><span class="o">&gt;</span> <span class="n">content</span><span class="o">;</span>
</pre></div>
</div>

  <h3>Save and Find</h3>
  <p>
    You put JSON content into the content property and save it and fetch is as normal.
  </p>
<div class="syntax java"><div class="highlight"><pre>  <span class="n">String</span> <span class="n">rawJson</span> <span class="o">=</span> <span class="s">&quot;{\&quot;docName\&quot;:\&quot;My document\&quot;, \&quot;docScore\&quot;:234, \&quot;title\&quot;:\&quot;Some title\&quot;}&quot;</span><span class="o">;</span>

  <span class="c1">// get the JSON into a map using Jackson or similar tool.</span>
  <span class="c1">// Ebean has EJson which using Jackson core which can be used</span>
  <span class="c1">// to parse JSON content</span>
  <span class="n">Map</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">Object</span><span class="o">&gt;</span> <span class="n">content</span> <span class="o">=</span> <span class="n">EJson</span><span class="o">.</span><span class="na">parseObject</span><span class="o">(</span><span class="n">rawJson</span><span class="o">);</span>

  <span class="n">SimpleDoc</span> <span class="n">doc</span> <span class="o">=</span> <span class="k">new</span> <span class="n">SimpleDoc</span><span class="o">();</span>
  <span class="n">doc</span><span class="o">.</span><span class="na">setName</span><span class="o">(</span><span class="s">&quot;doc1&quot;</span><span class="o">);</span>
  <span class="n">doc</span><span class="o">.</span><span class="na">setContent</span><span class="o">(</span><span class="n">content</span><span class="o">);</span>

  <span class="c1">// save to db</span>
  <span class="n">doc</span><span class="o">.</span><span class="na">save</span><span class="o">();</span>

  <span class="c1">// fetch from db</span>
  <span class="n">SimpleDoc</span> <span class="n">doc1</span> <span class="o">=</span> <span class="n">SimpleDoc</span><span class="o">.</span><span class="na">find</span><span class="o">.</span><span class="na">byId</span><span class="o">(</span><span class="n">doc</span><span class="o">.</span><span class="na">getId</span><span class="o">());</span>

  <span class="n">assertEquals</span><span class="o">(</span><span class="s">&quot;My document&quot;</span><span class="o">,</span> <span class="n">doc1</span><span class="o">.</span><span class="na">getContent</span><span class="o">().</span><span class="na">get</span><span class="o">(</span><span class="s">&quot;docName&quot;</span><span class="o">));</span>
</pre></div>
</div>

  <h3>Exists expression</h3>
  <p>
    Unfortunately the JSONB exists expression uses the "?" character which is the JDBC bind value character.
    This makes it difficult to use that expression via JDBC. To get around this issue the suggested solution
    is to create a database function like:
  </p>
<div class="syntax sql"><div class="highlight"><pre><span class="k">CREATE</span> <span class="k">FUNCTION</span> <span class="n">jsonbExists</span> <span class="p">(</span><span class="n">jsonb</span><span class="p">,</span> <span class="nb">text</span><span class="p">)</span> <span class="k">RETURNS</span> <span class="n">bool</span> <span class="k">AS</span> <span class="err">$$</span> <span class="k">SELECT</span> <span class="err">$</span><span class="mi">1</span> <span class="o">?</span> <span class="err">$</span><span class="mi">2</span><span class="p">;</span> <span class="err">$$</span> <span class="k">LANGUAGE</span> <span class="k">sql</span><span class="p">;</span>
</pre></div>
</div>
  <p>
    The function can then be used in an Ebean query like:
  </p>

<div class="syntax java"><div class="highlight"><pre><span class="n">List</span><span class="o">&lt;</span><span class="n">SimpleDoc</span><span class="o">&gt;</span> <span class="n">docs</span> <span class="o">=</span> <span class="n">SimpleDoc</span><span class="o">.</span><span class="na">find</span><span class="o">.</span><span class="na">where</span><span class="o">()</span>
  <span class="o">.</span><span class="na">raw</span><span class="o">(</span><span class="s">&quot;jsonbExists(content#&gt;&#39;{docName}&#39;, &#39;My document&#39;)&quot;</span><span class="o">)</span>
  <span class="o">.</span><span class="na">findList</span><span class="o">();</span>
</pre></div>
</div>
</p>
</div>

    </div>

    <div class="col-md-3" role="complementary">
      <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm">
        <ul class="nav bs-docs-sidenav">
<li >
  <a href="/docs/introduction">Introduction</a>
</li>
{'fraction_non_alphanumeric': 0.25042775990691946, 'fraction_numerical': 0.006501950585175552, 'mean_word_length': 4.536945812807882, 'pattern_counts': {'":': 0, '<': 731, '<?xml version=': 0, '>': 731, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7007103', 'n_tokens_mistral': 5130, 'n_tokens_neox': 4992, 'n_words': 962}