https://github.com/voomdoon/vd-from-string-parser
https://github.com/voomdoon/vd-from-string-parser
parsing
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/voomdoon/vd-from-string-parser
- Owner: voomdoon
- License: apache-2.0
- Created: 2024-03-20T05:37:46.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-12-19T21:26:57.000Z (6 months ago)
- Last Synced: 2025-12-22T09:38:09.593Z (6 months ago)
- Topics: parsing
- Language: Java
- Homepage:
- Size: 37.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# vd-from-string-parser
**Modular utility to parse Java objects from `String` values.**
This library supports a variety of common types and can be extended with custom parsers using Java’s Service Provider Interface (SPI).
This utility is used at [vd-from-properties-parser](https://github.com/voomdoon/vd-from-properties-parser) to support parsing objects from `Properties`.
## Features
- Built-in support for:
- Primitive types and their wrappers (`int`, `long`, `double`, `boolean`, etc.)
- `BigInteger`, `BigDecimal`
- `Pattern`, `Class`, `URL`, `URI`
- Enums
- Extensible via SPI (Service Provider Interface via `META-INF/services`)
- Simple, consistent API
- Validates and initializes custom parsers at runtime
---
## Usage
```java
FromStringParsers parsers = FromStringParsers.DEFAULT;
int number = parsers.parse("42", int.class);
URL url = parsers.parse("https://example.com", URL.class);
Pattern pattern = parsers.parse("\\d+", Pattern.class);
MyEnum value = parsers.parse("VALUE", MyEnum.class);
```
## Adding Custom Parsers
To support custom types, implement the `FromStringParser` interface:
```java
public class MyTypeParser implements FromStringParser {
@Override
public Class getResultClass() {
return MyType.class;
}
@Override
public MyType parse(String string) throws ParseException {
// convert string to MyType
}
}
```
Register the parser using Java’s SPI mechanism:
1. Create a file named
`META-INF/services/de.voomdoon.parser.fromstring.FromStringParser`
2. List your implementation class inside that file:
```
com.example.MyTypeParser
```
Custom parsers are discovered and loaded automatically.
---
## Error Handling
- If no parser is registered for a given type, `parse` throws `UnsupportedOperationException`.
- If a parser fails to declare its result class, an `InvalidParserException` is thrown during initialization.