Git Product home page Git Product logo

js-to-java's Introduction

js-to-java

NPM version build status Test coverage npm download

Easy way to wrap js object to java object.

In hessian.js, we need to write java classname with js object so make it encode as the write class.

Install

NPM

$ npm install js-to-java

Usage

Example

var java = require('js-to-java');

// Java: com.java.Object o = new com.java.Object();
java('com.java.Object', { foo: 'bar' });
// => {$class: 'com.java.Object', $: { foo: 'bar' }}

// Java: Boolean r;
java.Boolean(true);
// => {$class: 'java.lang.Boolean', $: true}

// Java: short[] shorts = new short[] {1, 2, 3};
java.array('short', [1, 2, 3]);
// => {$class: '[short', $: [1, 2, 3]}

// Java: int[] ints = new int[] {1, 2, 3};
java.array('int', [1, 2, 3]);
// same to the next example
java.array.int([1, 2, 3]);
// => {$class: '[int', $: [1, 2, 3]}

API

Type Mapping

  Boolean: 'java.lang.Boolean',
  boolean: 'boolean',
  Integer: 'java.lang.Integer',
  int: 'int',
  short: 'short',
  Short: 'java.lang.Short',
  byte: 'byte',
  Byte: 'java.lang.Byte',
  long: 'long',
  Long: 'java.lang.Long',
  double: 'double',
  Double: 'java.lang.Double',
  float: 'float',
  Float: 'java.lang.Float',
  String: 'java.lang.String',
  char: 'char',
  chars: 'char[]',
  Character: 'java.lang.Character',
  List: 'java.util.List',
  Set: 'java.util.Set',
  Collection: 'java.util.Collection',
  Iterator: 'java.util.Iterator',
  Enumeration: 'java.util.Enumeration',
  HashMap: 'java.util.HashMap',
  Map: 'java.util.Map',
  Dictionary: 'java.util.Dictionary'

java.abstract(abstractClassname, classname, value)

abstract class

java.abstract('com.demo.Parent', 'com.demo.Child', { foo: 'bar' });
// => { $abstractClass: 'com.demo.Parent', $class: 'com.demo.Child', $: { foo: 'bar' } }

java[.combine](classname, value)

Custom combineFunction:

java.combine('com.test.Object', { foo: 'bar' });
java('com.test.Object', { foo: 'bar' });
// => { $class: 'com.test.Object', $: { foo: 'bar' } }

java.Class(classname)

java.Class('java.lang.String');
// => { $class: 'java.lang.Class', $: { name: 'java.lang.String' } }

java.Locale(locale, handle)

java.Locale('zh_CN', ['com.caucho.hessian.io.LocaleHandle']);
// => { $class: 'com.caucho.hessian.io.LocaleHandle', $: { value: 'zh_CN' } }

java.BigDecimal(decimal)

java.BigDecimal('100.06');
// Or java.BigDecimal({value: '100.06'});
// => { $class: 'java.math.BigDecimal', $: { value: '100.06' } }

java.enum(classname, value)

java.enum('hessian.demo.Color', 'RED');
  or
java.enum('hessian.demo.Color', {name: 'RED'});
// => { $class: 'hessian.demo.Color', $: { name: 'RED' } }

java.array(classname, values)

java.array('Boolean', [true, false]);
// => { $class: '[java.lang.Boolean' $: [true, false] }

Available built-in classes shortcuts:

  • java.array.Boolean(values)
  • java.array.boolean(values)
  • java.array.Integer(values)
  • java.array.int(values)
  • java.array.short(values)
  • java.array.Short(values)
  • java.array.byte(values)
  • java.array.Byte(values)
  • java.array.long(values)
  • java.array.Long(values)
  • java.array.double(values)
  • java.array.Double(values)
  • java.array.float(values)
  • java.array.Float(values)
  • java.array.String(values)
  • java.array.char(values)
  • java.array.chars(values)
  • java.array.Character(values)
  • java.array.List(values)
  • java.array.Set(values)
  • java.array.Iterator(values)
  • java.array.Enumeration(values)
  • java.array.HashMap(values)
  • java.array.Map(values)
  • java.array.Dictionary(values)

java.exception(err[, className])

default className is java.lang.Exception.

{
  '$class': `${className}`,
  '$': {
    detailMessage: {
      '$class': 'java.lang.String',
      '$': `${err.name}: ${err.message}`,
    },
    stackTrace: {
      '$class': '[java.lang.StackTraceElement',
      '$': stackTraceElements,
    },
  },
}

java.revert(obj)

Wrap java object back to js object reversely.

var data = {
  $class: 'xxxx',
  $: {
    foo: 'bar',
    bar: {
      $class: 'int',
      $: 3,
    },
  },
};
java.revert(data);
// => {foo: 'bar', bar: 3}

License

MIT

js-to-java's People

Contributors

dead-horse avatar coolme200 avatar fengmk2 avatar greenkeeperio-bot avatar shaoshuai0102 avatar gxcsoccer avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.