JSON, SQLite & Persistence

    Daniel Oskarsson   Know IT
Preface


24 slides and Netbeans Demo

Contents are more towards avoiding pitfalls than learning the
basics.
"Simplicity"
JSON

json.org
http://www.json.org
+             -



json     size       validation




xml    validation     size
JAX-RS                                   JAX-WS



public class Tv {
  @GET
  @Path("/tv/")
  @Produces({"application/json"})
  public List<Channel> getChannels() {
    List<Channel> channels = getChannels();
    return channels;
  }
}
JAX-B

@XmlRootElement
public class Channel {
  public Channel() {}
  @XmlElement
  private String id;
  @XmlElement
  private String name;
  @XmlElement
  public String getLogo() {
    return String.format(path, id);
  }
  @XmlElement
  public List<Program> getPrograms() {
    return getPrograms();
  }
}
{
    "channel":[
      {
          "id":"svt1.svt.se",
          "name":"SVT1",
          "logo":"svt1.svt.se.png",
          "programs":[
              {
                   "name":"Kulturnyheterna",
                   "start":"2011-11-23T18:00:00Z",
                   "stop":"2011-11-23T18:15:00Z"
              },
              {
                   "name":"Regionala nyheter",
                   "start":"2011-11-23T18:15:00Z",
                   "stop":"2011-11-23T18:30:00Z"
              }
          ]
}]}
SQLite

sqlite.org
SQLite

"SQLite is a software library that implements a self-contained,
server-less, zero-configuration, transactional SQL database
engine"

"SQLite does not have a separate server process"

"SQLite reads and writes directly to ordinary disk files"

"Think of SQLite not as a replacement for Oracle but as a
replacement for fopen()"



                                   http://www.sqlite.org/about.html
Storage Classes

Values are one of the following storage classes:

  INTEGER (signed)
  REAL
  TEXT (UTF)
  NULL
  BLOB

BOOL (INTEGER 0 or INTEGER 1)
DATE (ISO8601 TEXT or Unix Time INTEGER or Julian days)
TIME (ISO8601 TEXT or Unix Time INTEGER or Julian days)


                            http://www.sqlite.org/datatype3.html
Type Affinity

Columns are one the following type affinities:

INTEGER
REAL
NUMERIC
TEXT (NULL, BLOB)
NONE

Internally values may be casted before stored




                            http://www.sqlite.org/datatype3.html
http://www.sqlite.org/lang_createtable.html
Foreign keys

"Foreign key constraints are disabled by default (for backwards
compatibility), so must be enabled separately for each
database connection separately." (http://www.sqlite.
org/foreignkeys.html)
Class.forName("org.sqlite.JDBC");
final String url = "jdbc:sqlite:json.db";

SQLiteConfig config = new SQLiteConfig();
config.enforceForeignKeys(true);

Connection connection = DriverManager.getConnection(url,
config.toProperties());
As an in-memory-database

Class.forName("org.sqlite.JDBC");
final String url = "jdbc:sqlite::memory:";
Connection connection = DriverManager.getConnection(url);
JDBC

http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC

<dependency>
  <groupId>org.xerial</groupId>
  <artifactId>sqlite-jdbc</artifactId>
</dependency>
1+1=3
JSON Persistence Framework

github.com/danieloskarsson/json-persistence
Characteristics

Key-value-based persistence

Zero configuration
  No database setup
  No mappings

No modifications of domain objects
  No serializable
  No annotations

Apache 2.0 License
Design decisions

Serialization & Deserialization
  JSON Serialization and Deserialization is NOT provided
  The user selects their favorite JSON library for the task

Versioning is NOT supported
  Some JSON libraries support versioning
  Some JSON libraries navigates the tree of the desired type
     As opposed to navigating the tree of the input

  The data type is used as the key / id
  E.g. tv.domain.Channel[] or tv.domain.Channel
  Multiple instance of the same class must be in a list
  JSON should be parsed before it is persisted
A perfect match with Smartphones
SmartPhones often...

Comes bundled with support for JSON
Comes bundled with support for SQLite
Receive JSON data from a REST call


JSON Persistence Framework is forthcoming on
  Android
  iOS
  Windows Phone
github.com/danieloskarsson

   linkedin.com/in/danieloskarsson

Json Persistence Framework

  • 1.
    JSON, SQLite &Persistence Daniel Oskarsson Know IT
  • 2.
    Preface 24 slides andNetbeans Demo Contents are more towards avoiding pitfalls than learning the basics.
  • 3.
  • 4.
  • 5.
  • 6.
    + - json size validation xml validation size
  • 7.
    JAX-RS JAX-WS public class Tv { @GET @Path("/tv/") @Produces({"application/json"}) public List<Channel> getChannels() { List<Channel> channels = getChannels(); return channels; } }
  • 8.
    JAX-B @XmlRootElement public class Channel{ public Channel() {} @XmlElement private String id; @XmlElement private String name; @XmlElement public String getLogo() { return String.format(path, id); } @XmlElement public List<Program> getPrograms() { return getPrograms(); } }
  • 9.
    { "channel":[ { "id":"svt1.svt.se", "name":"SVT1", "logo":"svt1.svt.se.png", "programs":[ { "name":"Kulturnyheterna", "start":"2011-11-23T18:00:00Z", "stop":"2011-11-23T18:15:00Z" }, { "name":"Regionala nyheter", "start":"2011-11-23T18:15:00Z", "stop":"2011-11-23T18:30:00Z" } ] }]}
  • 10.
  • 11.
    SQLite "SQLite is asoftware library that implements a self-contained, server-less, zero-configuration, transactional SQL database engine" "SQLite does not have a separate server process" "SQLite reads and writes directly to ordinary disk files" "Think of SQLite not as a replacement for Oracle but as a replacement for fopen()" http://www.sqlite.org/about.html
  • 12.
    Storage Classes Values areone of the following storage classes: INTEGER (signed) REAL TEXT (UTF) NULL BLOB BOOL (INTEGER 0 or INTEGER 1) DATE (ISO8601 TEXT or Unix Time INTEGER or Julian days) TIME (ISO8601 TEXT or Unix Time INTEGER or Julian days) http://www.sqlite.org/datatype3.html
  • 13.
    Type Affinity Columns areone the following type affinities: INTEGER REAL NUMERIC TEXT (NULL, BLOB) NONE Internally values may be casted before stored http://www.sqlite.org/datatype3.html
  • 14.
  • 15.
    Foreign keys "Foreign keyconstraints are disabled by default (for backwards compatibility), so must be enabled separately for each database connection separately." (http://www.sqlite. org/foreignkeys.html) Class.forName("org.sqlite.JDBC"); final String url = "jdbc:sqlite:json.db"; SQLiteConfig config = new SQLiteConfig(); config.enforceForeignKeys(true); Connection connection = DriverManager.getConnection(url, config.toProperties());
  • 16.
    As an in-memory-database Class.forName("org.sqlite.JDBC"); finalString url = "jdbc:sqlite::memory:"; Connection connection = DriverManager.getConnection(url);
  • 17.
  • 18.
  • 19.
  • 20.
    Characteristics Key-value-based persistence Zero configuration No database setup No mappings No modifications of domain objects No serializable No annotations Apache 2.0 License
  • 21.
    Design decisions Serialization &Deserialization JSON Serialization and Deserialization is NOT provided The user selects their favorite JSON library for the task Versioning is NOT supported Some JSON libraries support versioning Some JSON libraries navigates the tree of the desired type As opposed to navigating the tree of the input The data type is used as the key / id E.g. tv.domain.Channel[] or tv.domain.Channel Multiple instance of the same class must be in a list JSON should be parsed before it is persisted
  • 22.
    A perfect matchwith Smartphones
  • 23.
    SmartPhones often... Comes bundledwith support for JSON Comes bundled with support for SQLite Receive JSON data from a REST call JSON Persistence Framework is forthcoming on Android iOS Windows Phone
  • 24.
    github.com/danieloskarsson linkedin.com/in/danieloskarsson