Services to write Nit objects to JSON strings: serialize_to_json and JsonSerializer

Introduced classes

class JsonSerializer

json :: JsonSerializer

Serializer of Nit objects to Json string.

Redefined classes

redef enum Bool

json :: serialization_write $ Bool

Native Booleans.
redef enum Byte

json :: serialization_write $ Byte

Native bytes.
redef extern class CString

json :: serialization_write $ CString

C string char *
redef enum Char

json :: serialization_write $ Char

Native characters.
redef interface Collection[E: nullable Object]

json :: serialization_write $ Collection

The root of the collection hierarchy.
redef enum Float

json :: serialization_write $ Float

Native floating point numbers.
redef enum Int

json :: serialization_write $ Int

Native integer numbers.
redef interface Map[K: nullable Object, V: nullable Object]

json :: serialization_write $ Map

Maps are associative collections: key -> item.
redef interface Serializable

json :: serialization_write $ Serializable

Instances of this class can be passed to Serializer::serialize
redef interface SimpleCollection[E: nullable Object]

json :: serialization_write $ SimpleCollection

Items can be added to these collections.
redef abstract class Text

json :: serialization_write $ Text

High-level abstraction for all text representations

All class definitions

redef enum Bool

json :: serialization_write $ Bool

Native Booleans.
redef enum Byte

json :: serialization_write $ Byte

Native bytes.
redef extern class CString

json :: serialization_write $ CString

C string char *
redef enum Char

json :: serialization_write $ Char

Native characters.
redef interface Collection[E: nullable Object]

json :: serialization_write $ Collection

The root of the collection hierarchy.
redef enum Float

json :: serialization_write $ Float

Native floating point numbers.
redef enum Int

json :: serialization_write $ Int

Native integer numbers.
class JsonSerializer

json $ JsonSerializer

Serializer of Nit objects to Json string.
redef interface Map[K: nullable Object, V: nullable Object]

json :: serialization_write $ Map

Maps are associative collections: key -> item.
redef interface Serializable

json :: serialization_write $ Serializable

Instances of this class can be passed to Serializer::serialize
redef interface SimpleCollection[E: nullable Object]

json :: serialization_write $ SimpleCollection

Items can be added to these collections.
redef abstract class Text

json :: serialization_write $ Text

High-level abstraction for all text representations
package_diagram json::serialization_write serialization_write serialization::caching caching json::serialization_write->serialization::caching serialization::engine_tools engine_tools serialization::caching->serialization::engine_tools ...serialization::engine_tools ... ...serialization::engine_tools->serialization::engine_tools json::json json json::json->json::serialization_write serialization::custom_serialization custom_serialization serialization::custom_serialization->json::serialization_write json::json... ... json::json...->json::json a_star-m a_star-m a_star-m->serialization::custom_serialization a_star-m... ... a_star-m...->a_star-m

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module engine_tools

serialization :: engine_tools

Advanced services for serialization engines
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module meta

meta :: meta

Simple user-defined meta-level to manipulate types of instances as object.
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module serialization_core

serialization :: serialization_core

Abstract services to serialize Nit objects to different formats
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module caching

serialization :: caching

Services for caching serialization engines

Children

module custom_serialization

serialization :: custom_serialization

Example of an ad hoc serializer that is tailored to transform business specific objects into customized representation.
module json

json :: json

Read and write JSON formatted text using the standard serialization services

Descendants

module a_star-m

a_star-m

module api

github :: api

Nit object oriented interface to Github api.
module at_boot

android :: at_boot

Import this module to launch Service at device boot
module bundle

android :: bundle

A mapping class of String to various value types used by the
module cache

github :: cache

Enable caching on Github API accesses.
module client

gamnit :: client

Client-side network services for games and such
module common

gamnit :: common

Services common to the client and server modules
module commonmark_gen

markdown2 :: commonmark_gen

Generate Nitunit tests from commonmark specification.
module curl_json

neo4j :: curl_json

cURL requests compatible with the JSON REST APIs.
module curl_rest

curl :: curl_rest

module data_store

android :: data_store

Implements app::data_store using shared_preferences
module data_store

linux :: data_store

app::data_store implementation on GNU/Linux
module data_store

ios :: data_store

Implements app::data_store using NSUserDefaults
module error

neo4j :: error

Errors thrown by the neo4j library.
module events

github :: events

Events are emitted by Github Hooks.
module example_angular

popcorn :: example_angular

This is an example of how to use angular.js with popcorn
module github

github :: github

Nit wrapper for Github API
module graph

neo4j :: graph

Provides an interface for services on a Neo4j graphs.
module hooks

github :: hooks

Github hook event listening with nitcorn.
module http_request

android :: http_request

Android implementation of app:http_request
module http_request

linux :: http_request

Implementation of app::http_request using GDK and Curl
module http_request

ios :: http_request

Implementation of app::http_request for iOS
module http_request

app :: http_request

HTTP request services: AsyncHttpRequest and Text::http_get
module http_request_example

app :: http_request_example

Example for the app::http_request main service AsyncHttpRequest
module intent

android :: intent

Services allowing to launch activities and start/stop services using
module intent_api10

android :: intent_api10

Services allowing to launch activities and start/stop services using
module intent_api11

android :: intent_api11

Refines intent module to add API 11 services
module intent_api12

android :: intent_api12

Refines intent module to add API 12 services
module intent_api14

android :: intent_api14

Refines intent module to add API 14 services
module intent_api15

android :: intent_api15

Refines intent module to add API 15 services
module intent_api16

android :: intent_api16

Refines intent module to add API 16 services
module intent_api17

android :: intent_api17

Refines intent module to add API 17 services
module intent_api18

android :: intent_api18

Refines intent module to add API 18 services
module intent_api19

android :: intent_api19

Refines intent module to add API 19 services
module json_graph_store

neo4j :: json_graph_store

Provides JSON as a mean to store graphs.
module loader

github :: loader

module mongodb

mongodb :: mongodb

MongoDB Nit Driver.
module mpi

mpi :: mpi

Implementation of the Message Passing Interface protocol by wrapping OpenMPI
module msgpack

msgpack :: msgpack

MessagePack, an efficient binary serialization format
module msgpack_to_json

msgpack :: msgpack_to_json

Convert MessagePack format to JSON
module native_ui

android :: native_ui

Native services from the android.view and android.widget namespaces
module neo4j

neo4j :: neo4j

Neo4j connector through its JSON REST API using curl.
module network

gamnit :: network

Easy client/server logic for games and simple distributed applications
module nit_activity

android :: nit_activity

Core implementation of app.nit on Android using a custom Java entry point
module pop_auth

popcorn :: pop_auth

Authentification handlers.
module pop_json

popcorn :: pop_json

Introduce useful services for JSON REST API handlers.
module pop_repos

popcorn :: pop_repos

Repositories for data management.
module pop_templates

popcorn :: pop_templates

Template rendering for popcorn
module queries

mongodb :: queries

Mongo queries framework
module restful

nitcorn :: restful

Support module for the nitrestful tool and the restful annotation
module restful_annot

nitcorn :: restful_annot

Example for the restful annotation documented at lib/nitcorn/restful.nit
module sequential_id

neo4j :: sequential_id

Provides a sequential identification scheme for Neo4j nodes.
module serialization_read

msgpack :: serialization_read

Deserialize full Nit objects from MessagePack format
module server

gamnit :: server

Server-side network services for games and such
module service

android :: service

Android service support for app.nit centered around the class Service
module shared_preferences

android :: shared_preferences

Services allowing to save and load datas to internal android device
module shared_preferences_api10

android :: shared_preferences_api10

Services to save/load data using android.content.SharedPreferences for the android platform
module shared_preferences_api11

android :: shared_preferences_api11

Refines shared_preferences module to add API 11 services
module store

json :: store

Store and load json data.
module ui

linux :: ui

Implementation of the app.nit UI module for GNU/Linux
module ui

android :: ui

Views and services to use the Android native user interface
module ui_test

android :: ui_test

Test for app.nit's UI services
module wallet

github :: wallet

Github OAuth tokens management
module wifi

android :: wifi

Simple wrapper of the Android WiFi services
# Services to write Nit objects to JSON strings: `serialize_to_json` and `JsonSerializer`
module serialization_write

import ::serialization::caching
private import ::serialization::engine_tools

# Serializer of Nit objects to Json string.
class JsonSerializer
	super CachingSerializer

	# Target writing stream
	var stream: Writer

	# Write plain JSON? Standard JSON without metadata for deserialization
	#
	# If `false`, the default, serialize to support deserialization:
	#
	# * Write metadata, including the types of the serialized objects so they can
	#   be deserialized to their original form using `JsonDeserializer`.
	# * Use references when an object has already been serialized so to not duplicate it.
	# * Support cycles in references.
	# * Preserve the Nit `Char` and `Byte` types as special objects.
	# * The generated JSON is standard and can be read by non-Nit programs.
	#   However, some Nit types are not represented by the simplest possible JSON representation.
	#   With the added metadata, it can be complex to read.
	#
	# If `true`, serialize for other programs:
	#
	# * Nit objects are serialized to pure and standard JSON so they can
	#   be easily read by non-Nit programs and humans.
	# * Nit objects are serialized for every references, so they can be duplicated.
	#   It is easier to read but it creates a larger output.
	# * Does not support cycles, will replace the problematic references by `null`.
	# * Does not serialize the metadata needed to deserialize the objects
	#   back to regular Nit objects.
	# * Keys of Nit `HashMap` are converted to their string representation using `to_s`.
	var plain_json = false is writable

	# Write pretty JSON for human eyes?
	#
	# Toggles skipping lines between attributes of an object and
	# properly indent the written JSON.
	var pretty_json = false is writable

	# Current indentation level used for writing `pretty_json`
	private var indent_level = 0

	# List of the current open objects, the first is the main target of the serialization
	#
	# Used only when `plain_json == true` to detect cycles in serialization.
	private var open_objects = new Array[Object]

	# Has the first attribute of the current object already been serialized?
	#
	# Used only when `plain_json == true`.
	private var first_attribute = false

	redef var current_object = null

	redef fun serialize(object)
	do
		if object == null then
			stream.write "null"
		else
			if plain_json then
				for o in open_objects do
					if object.is_same_serialized(o) then
						# Cycle, can't be managed in plain json
						warn "Cycle detected in serialized object, replacing reference with 'null'."
						stream.write "null"
						return
					end
				end

				open_objects.add object
			end

			first_attribute = true
			var last_object = current_object
			current_object = object
			object.accept_json_serializer self
			first_attribute = false
			current_object = last_object

			if plain_json then open_objects.pop
		end
	end

	redef fun serialize_attribute(name, value)
	do
		if not plain_json or not first_attribute then
			stream.write ","
		end
		first_attribute = false

		new_line_and_indent
		stream.write "\""
		stream.write name
		stream.write "\":"
		if pretty_json then stream.write " "
		super
	end

	redef fun serialize_reference(object)
	do
		if not plain_json and cache.has_object(object) then
			# if already serialized, add local reference
			var id = cache.id_for(object)
			stream.write "\{"
			indent_level += 1
			new_line_and_indent
			stream.write "\"__kind\": \"ref\", \"__id\": "
			stream.write id.to_s
			indent_level -= 1
			new_line_and_indent
			stream.write "\}"
		else
			# serialize here
			serialize object
		end
	end

	# Write a new line and indent it, only if `pretty_json`
	private fun new_line_and_indent
	do
		if pretty_json then
			stream.write "\n"
			for i in indent_level.times do stream.write "\t"
		end
	end
end

redef class Text

	redef fun accept_json_serializer(v)
	do
		v.stream.write "\""

		var start_i = 0
		var escaped = null
		for i in [0 .. self.length[ do
			var char = self[i]
			if char == '\\' then
				escaped = "\\\\"
			else if char == '\"' then
				escaped = "\\\""
			else if char < ' ' then
				if char == '\n' then
					escaped = "\\n"
				else if char == '\r' then
					escaped = "\\r"
				else if char == '\t' then
					escaped = "\\t"
				else
					escaped = char.escape_to_utf16
				end
			end

			if escaped != null then
				# Write open non-escaped string
				if start_i <= i then
					v.stream.write substring(start_i, i-start_i)
				end

				# Write escaped character
				v.stream.write escaped
				escaped = null
				start_i = i+1
			end
		end

		# Write remaining non-escaped string
		if start_i < length then
			if start_i == 0 then
				v.stream.write self
			else
				v.stream.write substring(start_i, length-start_i)
			end
		end

		v.stream.write "\""
	end
end

redef class Serializable

	# Serialize `self` to JSON
	#
	# Set `plain = true` to generate standard JSON, without deserialization metadata.
	# Use this option if the generated JSON will be read by other programs or humans.
	# Use the default, `plain = false`, if the JSON is to be deserialized by a Nit program.
	#
	# Set `pretty = true` to generate pretty JSON for human eyes.
	# Use the default, `pretty = false`, to generate minified JSON.
	#
	# This method should not be refined by subclasses,
	# instead `accept_json_serializer` can customize the serialization of an object.
	#
	# See: `JsonSerializer`
	fun serialize_to_json(plain, pretty: nullable Bool): String
	do
		var stream = new StringWriter
		var serializer = new JsonSerializer(stream)
		serializer.plain_json = plain or else false
		serializer.pretty_json = pretty or else false
		serializer.serialize self
		stream.close
		return stream.to_s
	end

	# Serialize `self` to plain JSON
	#
	# Compatibility alias for `serialize_to_json(plain=true)`.
	fun to_json: String do return serialize_to_json(plain=true)

	# Serialize `self` to plain pretty JSON
	#
	# Compatibility alias for `serialize_to_json(plain=true, pretty=true)`.
	fun to_pretty_json: String do return serialize_to_json(plain=true, pretty=true)

	# Refinable service to customize the serialization of this class to JSON
	#
	# This method can be refined to customize the serialization by either
	# writing pure JSON directly on the stream `v.stream` or
	# by using other services of `JsonSerializer`.
	#
	# Most of the time, it is preferable to refine the method `core_serialize_to`
	# which is used by all the serialization engines, not just JSON.
	protected fun accept_json_serializer(v: JsonSerializer)
	do
		v.stream.write "\{"
		v.indent_level += 1
		if not v.plain_json then
			var id = v.cache.new_id_for(self)
			v.new_line_and_indent
			v.stream.write "\"__kind\": \"obj\", \"__id\": "
			v.stream.write id.to_s
			v.stream.write ", \"__class\": \""
			v.stream.write class_name
			v.stream.write "\""
		end
		v.serialize_core(self)

		v.indent_level -= 1
		v.new_line_and_indent
		v.stream.write "\}"
	end
end

redef class Int
	redef fun accept_json_serializer(v) do v.stream.write to_s
end

redef class Float
	redef fun accept_json_serializer(v) do v.stream.write to_s
end

redef class Bool
	redef fun accept_json_serializer(v) do v.stream.write to_s
end

redef class Char
	redef fun accept_json_serializer(v)
	do
		if v.plain_json then
			to_s.accept_json_serializer v
		else
			v.stream.write "\{\"__kind\": \"char\", \"__val\": "
			to_s.accept_json_serializer v
			v.stream.write "\}"
		end
	end
end

redef class Byte
	redef fun accept_json_serializer(v)
	do
		if v.plain_json then
			to_i.accept_json_serializer v
		else
			v.stream.write "\{\"__kind\": \"byte\", \"__val\": "
			to_i.accept_json_serializer v
			v.stream.write "\}"
		end
	end
end

redef class CString
	redef fun accept_json_serializer(v) do to_s.accept_json_serializer(v)
end

redef class Collection[E]
	# Utility to serialize a normal Json array
	private fun serialize_to_pure_json(v: JsonSerializer)
	do
		v.stream.write "["
		var is_first = true
		for e in self do
			if is_first then
				is_first = false
			else
				v.stream.write ","
				if v.pretty_json then v.stream.write " "
			end

			if not v.try_to_serialize(e) then
				assert e != null # null would have been serialized
				v.warn("element of type {e.class_name} is not serializable.")
				v.stream.write "null"
			end
		end
		v.stream.write "]"
	end
end

redef class SimpleCollection[E]
	redef fun accept_json_serializer(v)
	do
		if v.plain_json then
			serialize_to_pure_json v
		else
			# Register as pseudo object
			var id = v.cache.new_id_for(self)
			v.stream.write """{"""
			v.indent_level += 1
			v.new_line_and_indent
			v.stream.write """"__kind": "obj", "__id": """
			v.stream.write id.to_s
			v.stream.write """, "__class": """"
			v.stream.write class_name
			v.stream.write """","""
			v.new_line_and_indent

			v.stream.write """"__items": """
			serialize_to_pure_json v

			core_serialize_to v

			v.indent_level -= 1
			v.new_line_and_indent
			v.stream.write "\}"
		end
	end
end

redef class Map[K, V]
	redef fun accept_json_serializer(v)
	do
		# Register as pseudo object
		var id = v.cache.new_id_for(self)

		v.stream.write "\{"
		v.indent_level += 1

		if v.plain_json then
			var first = true
			for key, val in self do
				if not first then
					v.stream.write ","
				else first = false
				v.new_line_and_indent

				var k = key or else "null"
				k.to_s.accept_json_serializer v
				v.stream.write ":"
				if v.pretty_json then v.stream.write " "
				if not v.try_to_serialize(val) then
					assert val != null # null would have been serialized
					v.warn("element of type {val.class_name} is not serializable.")
					v.stream.write "null"
				end
			end
		else
			v.new_line_and_indent
			v.stream.write """"__kind": "obj", "__id": """
			v.stream.write id.to_s
			v.stream.write """, "__class": """"
			v.stream.write class_name
			v.stream.write """", "__length": """
			v.stream.write length.to_s

			v.stream.write ","
			v.new_line_and_indent
			v.stream.write """"__keys": """
			keys.serialize_to_pure_json v

			v.stream.write ","
			v.new_line_and_indent
			v.stream.write """"__values": """
			values.serialize_to_pure_json v

			core_serialize_to v
		end

		v.indent_level -= 1
		v.new_line_and_indent
		v.stream.write "\}"
	end
end
lib/json/serialization_write.nit:15,1--413,3