Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
Rhino JavaScript Java Library Source Code
Rhino JavaScript Java Library is an open-source implementation of JavaScript
written entirely in Java.
Rhino JavaScript Java Library Source Code files are provided in binary package (rhino-1.7.14.zip).
You can also browse the source code below:
✍: FYIcenter.com
⏎ org/mozilla/javascript/JavaToJSONConverters.java
/* -*- Mode: java; tab-width: 4; indent-tabs-mode: 1; c-basic-offset: 4 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.javascript;
import java.beans.BeanDescriptor;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.function.UnaryOperator;
/**
* This class provides implementations of converters for Java objects to be used by the
* JSON.stringify method.
*
* <p>JSON.stringify will automatically convert instances of java.util.Map to javascript objects.
* Instances of java.util.Collection and java Arrays will be converted to javascript arrays.
*
* <p>This is a final effort at conversion for other java objects that appear as values, and may be
* preempted by objects which define a toJSON() method or by a replacer function passed to
* JSON.stringify. The return value will, in turn, be converted according to {@link
* Context#javaToJS} and stringified.
*
* @author Tony Germano
*/
public class JavaToJSONConverters {
private JavaToJSONConverters() {}
/** Convert Object to its toString() value. */
public static final UnaryOperator<Object> STRING = o -> o.toString();
/** Always return undefined */
public static final UnaryOperator<Object> UNDEFINED = o -> Undefined.instance;
/** Always return an empty object */
public static final UnaryOperator<Object> EMPTY_OBJECT = o -> Collections.EMPTY_MAP;
/** Throw a TypeError naming the class that could not be converted */
public static final UnaryOperator<Object> THROW_TYPE_ERROR =
o -> {
throw ScriptRuntime.typeErrorById(
"msg.json.cant.serialize", o.getClass().getName());
};
/**
* Convert JavaBean to an object as long as it has at least one readable property
*
* <p>If unable to determine properties or if none exist, null is returned. This method can be
* called from other converters to provide an alternate value on a returned null.
*/
public static final UnaryOperator<Object> BEAN =
value -> {
BeanInfo beanInfo;
try {
beanInfo = Introspector.getBeanInfo(value.getClass(), Object.class);
} catch (IntrospectionException e) {
return null;
}
LinkedHashMap<String, Object> properties = new LinkedHashMap<String, Object>();
for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
if (descriptor.getReadMethod() == null) continue;
Object propValue;
try {
propValue = descriptor.getReadMethod().invoke(value);
} catch (Exception e) {
continue;
}
properties.put(descriptor.getName(), propValue);
}
if (properties.size() == 0) return null;
LinkedHashMap<String, Object> obj = new LinkedHashMap<String, Object>();
BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
obj.put("beanClass", beanDescriptor.getBeanClass().getName());
obj.put("properties", properties);
return obj;
};
}
⏎ org/mozilla/javascript/JavaToJSONConverters.java
Or download all of them as a single archive file:
File name: rhino-1.7.14-sources.jar File size: 1029165 bytes Release date: 2022-01-06 Download
⇒ Example code to Test rhino-runtime-1.7.14.jar
⇐ Download Rhino JavaScript Binary Package
2022-05-03, ≈133🔥, 1💬
Popular Posts:
What Is jsse.jar (JDK 6) Java Secure Socket Extension? jsse.jar, Java Secure Socket Extension, is Ja...
How to download and install iText7-Core-7.1.4.zip? iText7-Core-7.1.4.zip is the binary package of iT...
Jackson is "the Java JSON library" or "the best JSON parser for Java". Or simply as "JSON for Java"....
JDK 17 java.compiler.jmod is the JMOD file for JDK 17 Compiler module. JDK 17 Compiler module compil...
JDK 17 java.security.jgss.jmod is the JMOD file for JDK 17 Security JGSS (Java Generic Security Serv...