Home

Awesome

js-to-java

NPM version CI 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.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

<!-- GITCONTRIBUTOR_START -->

Contributors

<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub><br/><img src="https://avatars.githubusercontent.com/u/1400114?v=4" width="100px;"/><br/><sub><b>coolme200</b></sub><br/><img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub><br/><img src="https://avatars.githubusercontent.com/u/14790466?v=4" width="100px;"/><br/><sub><b>greenkeeperio-bot</b></sub><br/><img src="https://avatars.githubusercontent.com/u/456108?v=4" width="100px;"/><br/><sub><b>shaoshuai0102</b></sub><br/><img src="https://avatars.githubusercontent.com/u/1207064?v=4" width="100px;"/><br/><sub><b>gxcsoccer</b></sub><br/>
<img src="https://avatars.githubusercontent.com/u/17831480?v=4" width="100px;"/><br/><sub><b>mytEcust</b></sub><br/><img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub><br/><img src="https://avatars.githubusercontent.com/u/15973504?v=4" width="100px;"/><br/><sub><b>zhaoleipeng</b></sub><br/>

This project follows the git-contributor spec, auto updated at Tue Jul 11 2023 11:39:26 GMT+0800.

<!-- GITCONTRIBUTOR_END -->