Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ic4j/ic4j-candid
Java Candid for The Internet Computer (IC4J) is a set of native Java libraries to manage the Internet Computer Candid types
https://github.com/ic4j/ic4j-candid
android dfinity icp jackson java jaxb jdbc json xml
Last synced: 5 days ago
JSON representation
Java Candid for The Internet Computer (IC4J) is a set of native Java libraries to manage the Internet Computer Candid types
- Host: GitHub
- URL: https://github.com/ic4j/ic4j-candid
- Owner: ic4j
- License: apache-2.0
- Created: 2022-01-15T00:05:47.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-09-16T21:03:10.000Z (about 2 months ago)
- Last Synced: 2024-09-17T02:31:10.882Z (about 2 months ago)
- Topics: android, dfinity, icp, jackson, java, jaxb, jdbc, json, xml
- Language: Java
- Homepage:
- Size: 245 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-internet-computer - ic4j-candid - Java library for Candid. (Candid / Candid implementations)
README
# dfinity-agent
Java Candid for The Internet Computer (IC4J) is a collection of open-source native Java libraries designed to handle Candid types on the Internet Computer.
Full documentation
hereThe code is implementation of the Internet Computer Interface protocol
https://sdk.dfinity.org/docs/interface-spec/index.htmland it's using Dfinity Rust agent as an inspiration, using similar package structures and naming conventions.
https://github.com/dfinity/agent-rs# License
IC4J Candid is available under Apache License 2.0.
# Documentation
## Supported type mapping between Java and Candid
| Candid | Java |
| :---------- | :---------- |
| bool | Boolean |
| int| BigInteger |
| int8 | Byte |
| int16 | Short |
| int32 | Integer |
| int64 | Long |
| nat| BigInteger |
| nat8 | Byte |
| nat16 | Short |
| nat32 | Integer |
| nat64 | Long |
| float32 | Float, Double |
| float64 | Double |
| text | String |
| opt | Optional |
| principal | Principal |
| vec | array, List |
| record | Map, Class |
| variant | Map, Enum |
| func | Func |
| service | Service |
| null |Null |## POJO Java class with Candid annotations
```
import java.math.BigInteger;import org.ic4j.candid.annotations.Field;
import org.ic4j.candid.annotations.Name;
import org.ic4j.candid.types.Type;public class Pojo {
@Field(Type.BOOL)
@Name("bar")
public Boolean bar;@Field(Type.INT)
@Name("foo")
public BigInteger foo;
}
``````
Pojo pojoValue = new Pojo();
pojoValue.bar = new Boolean(false);
pojoValue.foo = BigInteger.valueOf(43);
```## JSON (Jackson) serialization and deserialization
Use JacksonSerializer to serialize Jackson JsonNode or Jackson compatible Pojo class to Candid
```
JsonNode jsonValue;
IDLType idlType;IDLValue idlValue = IDLValue.create(jsonValue, JacksonSerializer.create(idlType));
List args = new ArrayList();
args.add(idlValue);IDLArgs idlArgs = IDLArgs.create(args);
byte[] buf = idlArgs.toBytes();
```Use JacksonDeserializer to deserialize Candid to Jackson JsonNode or Jackson compatible Pojo class
```
JsonNode jsonResult = IDLArgs.fromBytes(buf).getArgs().get(0)
.getValue(JacksonDeserializer.create(idlValue.getIDLType()), JsonNode.class);
```## XML (DOM) serialization and deserialization
Use DOMSerializer to serialize DOM Node to Candid
```
Node domValue;IDLValue idlValue = IDLValue.create(domValue,DOMSerializer.create());
List args = new ArrayList();
args.add(idlValue);IDLArgs idlArgs = IDLArgs.create(args);
byte[] buf = idlArgs.toBytes();
```Use DOMDeserializer to deserialize Candid to DOM Node
```
DOMDeserializer domDeserializer = DOMDeserializer.create(idlValue.getIDLType()).rootElement("http://scaleton.com/dfinity/candid","data");
Node domResult = IDLArgs.fromBytes(buf).getArgs().get(0).getValue(domDeserializer, Node.class);
```## JDBC (ResultSet) serialization
Use JDBCSerializer to serialize JDBC ResultSet to Candid
```
ResultSet result = statement.executeQuery(sql);
IDLValue idlValue = IDLValue.create(result, JDBCSerializer.create());
List args = new ArrayList();
args.add(idlValue);IDLArgs idlArgs = IDLArgs.create(args);
byte[] buf = idlArgs.toBytes();
```# Downloads / Accessing Binaries
To add Java IC4J Candid library to your Java project use Maven or Gradle import from Maven Central.
https://search.maven.org/artifact/ic4j/ic4j-candid/0.7.4/jar```
org.ic4j
ic4j-candid
0.7.4```
```
implementation 'org.ic4j:ic4j-candid:0.7.4'
```## Dependencies
This this is using these open source libraries
### Jackson JSON Serializer and Deserializer
To manage Jackson objects.### Java CC
To parse IC IDL Candid files# Build
You need JDK 8+ to build IC4J Candid.