North Pittsburgh Meatpackers

    js-to-java
    DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/js-to-java package

    2.7.0 • Public • Published

    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

    Install

    npm i js-to-java

    DownloadsWeekly Downloads

    212

    Version

    2.7.0

    License

    MIT

    Unpacked Size

    24.1 kB

    Total Files

    7

    Last publish

    Collaborators

    • gxcsoccer
    • dead_horse
    • coolme200
    • fengmk2