Documentation for Android

Usage Sample

The following code shows typical usage of the parser library.

import com.scandit.parser.*;
import org.json.*;
import java.util.HashMap;

...
...

// picker: BarcodePicker object from Scandit SDK
Parser p = picker.createParserForFormat(DataFormat.GS1AI);

ParserResult res = null;
try {
    res = parser.parseString("1719060110SCANDIT123");
} catch (IllegalArgumentException e) {
    // Something went wrong.
}


// We can either acces the data from the raw JSON string ...
Log.d(TAG, res.getJsonString());

// ... or from the fields dictionary ...
HashMap<String, Field> map = res.getFieldsDict();
Field metaField = map.get("17");
Log.d(TAG, metaField.getRaw());
// parsed can be a Map<String,Object>, Object[], Integer, Long, Boolean, Double or String
Object parsed = metaField.getParsed();
Log.d(TAG, metaField.getRaw());
Log.d(TAG, parsed.toString());

// ... or from the fields array
Field[] fields = res.getFieldsArray();
for (int i = 0; i < fields.length; i++) {
    Log.d(TAG, fields[i].getName());
    Log.d(TAG, fields[i].getRaw());
    Object p = fields[i].getParsed();
    Log.d(TAG, p.toString());
}

// Using the parser tools class we can easily extract dates from corresponding fields
Field dateField = map.get("17");
Calendar date = ParserTools.dateFromParsedObject(dateField.getParsed());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Log.d(TAG, format.format(date.getTime()));

Some data formats allow for special parsing options which modify the behaviour of the parser. These can be set in the following way:

Map opts = new HashMap<String, Object>();
opts.put("outputHumanReadableString", new Boolean(true));
parser.setOptions(opts);

Classes

class Parser

Defines the interface for a data string parser. To instantiate the parser, you must specify the type of the data format.

The parser type can be DataFormat.HIBC, DataFormat.GS1AI or DataFormat.DLID.

ParserResult parseString(String data_string)

Parses the data string and returns the contained fields in the result object. In case the result could not be parsed, then a java.lang.IllegalArgumentException is raised.

Returns:The parsed result
Return type:ParserResult
ParserResult parseRawData(byte[] data)

Parses the data in form of raw bytes and returns the contained fields in the result object. In case the result could not be parsed, then a java.lang.IllegalArgumentException is raised.

Returns:The parsed result
Return type:ParserResult
void setOptions(Map<String, Object> options)

Apply the option map to the parser, allowing the user to fine-tune the behaviour of the parser. Available options depend on the data format and are specified in the respective documentation. If the options object is invalid, an java.lang.IllegalArgumentException is raised.

class ParserResult
getJsonString()
Returns:A JSON string holding the complete result. The returned string always holds an array as the top-level construct. Thus, you can convert the string into an org.json.JSONArray or use any other JSON parser of your choice. See Structure of JSON Result Object for a description of the format.
Return type:String
getFieldsDict()
Returns:A hashmap of the Field objects with key being the field name.
Return type:HashMap<String,Field>
getFieldsArray();
Returns:An array of the Field objects. Use this accessor if the order of the fields in the string matter. If you are only interested in a particular field, using the ParserResult.getFieldsDict is simpler.
Return type:Field[]
class Field
getName()
Returns:The name of the field
Return type:String
getParsed()
Returns:The parsed representation of the data contained in this field. If no parsed representation is available for the field, this property is null. Use Field.getRaw() to retrieve the data in this case.
Return type:java.lang.Object. Depending on the field, this may be an instance of Object[], HashMap<String,Object>, Integer, Long, Boolean, Double or String

This object corresponds to the JSON “parsed” field.

getRaw()
Returns:The raw substring of the original code containing the field data. For fields that are inferred and do not have a direct correspondence to a particular part of the string, the string is set to an empty string. An example of such a field is the Metadata field field of HIBC codes
Return type:String
class ParserTools

This class contains methods that facilitate the data extraction from parsed objects.

static Calendar dateFromParsedObject(Object parsed)

This function takes the parsed data from a field (usually retrieved from Field.getParsed) and if possible extracts the date information from it. This only works if the object contains the “year”, “month” and “day” keys whereas “year” must be fully specified (four digits, not two digits). Most notably the date from the HIBC secondary data fields will not work because of this.

Return type:java.util.Calendar