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 to support parsing objects from Properties
.
- Built-in support for:
- Primitive types and their wrappers (
int
,long
,double
,boolean
, etc.) BigInteger
,BigDecimal
Pattern
,Class
,URL
,URI
- Enums
- Primitive types and their wrappers (
- Extensible via SPI (Service Provider Interface via
META-INF/services
) - Simple, consistent API
- Validates and initializes custom parsers at runtime
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);
To support custom types, implement the FromStringParser<T>
interface:
public class MyTypeParser implements FromStringParser<MyType> {
@Override
public Class<MyType> getResultClass() {
return MyType.class;
}
@Override
public MyType parse(String string) throws ParseException {
// convert string to MyType
}
}
Register the parser using Java’s SPI mechanism:
-
Create a file named
META-INF/services/de.voomdoon.parser.fromstring.FromStringParser
-
List your implementation class inside that file:
com.example.MyTypeParser
Custom parsers are discovered and loaded automatically.
- If no parser is registered for a given type,
parse
throwsUnsupportedOperationException
. - If a parser fails to declare its result class, an
InvalidParserException
is thrown during initialization.