serpent

Serpent serializes an object tree into a Python ast.literal_eval() compatible literal expression. It is safe to send serpent data to other machines over the network for instance (because only 'safe' literals are encoded). There is also a deserializer or parse provided that turns such a literal expression back into the appropriate Java object tree. It is an alternative to JSON to provide easy data integration between Java and Python. Serpent is more expressive as JSON (it supports more data types).

License

License

Categories

Categories

Net
GroupId

GroupId

net.razorvine
ArtifactId

ArtifactId

serpent
Last Version

Last Version

1.40
Release Date

Release Date

Type

Type

jar
Description

Description

serpent
Serpent serializes an object tree into a Python ast.literal_eval() compatible literal expression. It is safe to send serpent data to other machines over the network for instance (because only 'safe' literals are encoded). There is also a deserializer or parse provided that turns such a literal expression back into the appropriate Java object tree. It is an alternative to JSON to provide easy data integration between Java and Python. Serpent is more expressive as JSON (it supports more data types).
Project URL

Project URL

https://github.com/irmen/Serpent
Source Code Management

Source Code Management

https://github.com/irmen/Serpent

Download serpent

How to add to project

<!-- https://jarcasting.com/artifacts/net.razorvine/serpent/ -->
<dependency>
    <groupId>net.razorvine</groupId>
    <artifactId>serpent</artifactId>
    <version>1.40</version>
</dependency>
// https://jarcasting.com/artifacts/net.razorvine/serpent/
implementation 'net.razorvine:serpent:1.40'
// https://jarcasting.com/artifacts/net.razorvine/serpent/
implementation ("net.razorvine:serpent:1.40")
'net.razorvine:serpent:jar:1.40'
<dependency org="net.razorvine" name="serpent" rev="1.40">
  <artifact name="serpent" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.razorvine', module='serpent', version='1.40')
)
libraryDependencies += "net.razorvine" % "serpent" % "1.40"
[net.razorvine/serpent "1.40"]

Dependencies

test (1)

Group / Artifact Type Version
junit : junit jar 4.13.1

Project Modules

There are no modules declared in this project.

Serpent serialization library (Python/.NET/Java)

saythanks Build Status Latest Version Maven Central NuGet Anaconda-Server Badge

Serpent provides ast.literal_eval() compatible object tree serialization. It serializes an object tree into bytes (utf-8 encoded string) that can be decoded and then passed as-is to ast.literal_eval() to rebuild it as the original object tree. As such it is safe to send serpent data to other machines over the network for instance (because only 'safe' literals are encoded).

More info on Pypi: https://pypi.python.org/pypi/serpent Source code is on Github: https://github.com/irmen/Serpent

Copyright by Irmen de Jong ([email protected]) This software is released under the MIT software license. This license, including disclaimer, is available in the 'LICENSE' file.

PYTHON

Compatible with Python 3.6+ (use a serpent version before 1.30 for Python 2.7 support) It can be found on Pypi as 'serpent': https://pypi.python.org/pypi/serpent Example usage can be found in ./tests/example.py

C#/.NET

Package is available on www.nuget.org as 'Razorvine.Serpent'. Full source code can be found in ./dotnet/ directory. Example usage can be found in ./dotnet/Serpent.Test/Example.cs The project is a dotnet core project targeting NetStandard 2.0.

JAVA

Maven-artefact is available on maven central, groupid 'net.razorvine' artifactid 'serpent'. Full source code can be found in ./java/ directory. Example usage can be found in ./java/test/SerpentExample.java Versions before 1.23 require Java 7 or Java 8 (JDK 1.7 or 1.8) to compile and run. Version 1.23 and later require Java 8 (JDK 1.8) at a minimum to compile and run.

SOME MORE DETAILS

Serpent handles several special Python types to make life easier:

  • bytes, bytearrays, memoryview --> string, base-64 (or bytes-literal if selected) (you'll have to manually un-base64. Can use serpent.tobytes function for that.)
  • uuid.UUID, datetime.{datetime, date, time, timespan} --> appropriate string/number
  • decimal.Decimal --> string (to not lose precision)
  • array.array typecode 'u' --> string
  • array.array other typecode --> list
  • Exception --> dict with some fields of the exception (message, args)
  • collections module types --> mostly equivalent primitive types or dict
  • enums --> the value of the enum
  • namedtuple --> treated as just a tuple
  • attr dataclasses and python 3.7 native dataclasses: treated as just a class, so will become a dict
  • all other types --> dict with the __getstate__ or vars() of the object, and a __class__ element with the name of the class

Notes:

The serializer is not thread-safe. Make sure you're not making changes to the object tree that is being serialized, and don't use the same serializer in different threads.

Because the serialized format is just valid Python source code, it can contain comments. Serpent does not add comments by itself apart from the single header line.

Floats +inf and -inf are handled via a trick, Float 'nan' cannot be handled and is represented by the special value: {'__class__':'float','value':'nan'} We chose not to encode it as just the string 'NaN' because that could cause memory issues when used in multiplications.

Versions

Version
1.40
1.30
1.23
1.18
1.17
1.16
1.15
1.13
1.12
1.11