npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-to-java

v2.8.0

Published

easy way to wrap js object to java object

Downloads

650

Readme

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