# FeatureSerializer

Artifact: [androidx.graphics:graphics-shapes](https://developer.android.com/jetpack/androidx/releases/graphics) [View Source](https://cs.android.com/search?q=file:androidx/graphics/shapes/FeatureSerializer.kt+class:androidx.graphics.shapes.FeatureSerializer) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.1.0)

*** ** * ** ***


```
public final class FeatureSerializer
```

<br />

*** ** * ** ***

The `https://developer.android.com/reference/androidx/graphics/shapes/FeatureSerializer` is used to both serialize and parse `https://developer.android.com/reference/androidx/graphics/shapes/Feature` objects. This is beneficial when you want to re-use `https://developer.android.com/reference/androidx/graphics/shapes/RoundedPolygon` objects created by `https://developer.android.com/reference/androidx/graphics/shapes/SvgPathParser`, as parsing serialized `https://developer.android.com/reference/androidx/graphics/shapes/Feature` objects is more performant than using the svg path import.

**Example:**

```
// Do the following three *once*
val triangleSVGPath: String = "M0,0 0.5,1 1,0Z"
val triangleFeatures: List<Feature> = SvgPathParser.parseFeatures(triangleSVGPath)
val serializedTriangle: String = FeatureSerializer.serialize(triangleFeatures)

// Parse the serialized triangle features in your production code.
// You can adjust them (e.g. the type) however you want before parsing.
val features: List<Feature> = FeatureSerializer.parse(serializedTriangle)
val triangle: RoundedPolygon = RoundedPolygon(features, centerX = 0.5f, centerY = 0.5f)
Morph(triangle, ...)
```

## Summary

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/graphics/shapes/Feature>` | `https://developer.android.com/reference/androidx/graphics/shapes/FeatureSerializer#parse(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html serializedFeatures)` Parses a serialized string representation of `https://developer.android.com/reference/androidx/graphics/shapes/Feature` into their object representations, adhering to version 1 of the feature serialization format. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/graphics/shapes/FeatureSerializer#serialize(kotlin.collections.List)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/graphics/shapes/Feature> features)` Serializes a list of `https://developer.android.com/reference/androidx/graphics/shapes/Feature` objects into a string representation, adhering to version 1 of the feature serialization format. |

## Public methods

### parse

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.1.0)

```
public static final @NonNull List<@NonNull Feature> parse(@NonNull String serializedFeatures)
```

Parses a serialized string representation of `https://developer.android.com/reference/androidx/graphics/shapes/Feature` into their object representations, adhering to version 1 of the feature serialization format.

The serialized string must adhere to the format generated by `https://developer.android.com/reference/androidx/graphics/shapes/FeatureSerializer#serialize(kotlin.collections.List)`. This format consists of:

1. **Version Identifier:** A 'V' followed by the version number (e.g., 'V1').

2. **Feature Serialization:** Each `https://developer.android.com/reference/androidx/graphics/shapes/Feature` is serialized individually and concatenated.

   - **Feature Tag:** A single-character tag identifies the feature type:

     - 'n': Edge

     - 'x': Convex Corner

     - 'o': Concave Corner

     - Any other tags unknown to version 1 will default to an edge interpretation.

   - **Cubic Serialization:** Each `https://developer.android.com/reference/androidx/graphics/shapes/Cubic` within a feature is serialized as a sequence of comma-separated x,y coordinates. Subsequent cubics within a feature omit their first anchor points, as they're identical to their predecessors' last anchor points.

**Note:** The current version (1) of the serialization format is stable. However, future versions may introduce incompatible changes with version 1. The default behavior for unknown or missing versions will be version 1 parsing. If you have a later version string, update the library to the latest version.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html serializedFeatures` | The serialized `https://developer.android.com/reference/androidx/graphics/shapes/Feature` objects in a concatenated String. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/graphics/shapes/Feature>` | A list of parsed `https://developer.android.com/reference/androidx/graphics/shapes/Feature` objects, corresponding to the serialized input. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | - if a serialized string lacks sufficient points to create `https://developer.android.com/reference/androidx/graphics/shapes/Cubic` objects with |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | - if no feature tags could be found |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-number-format-exception/index.html` | - if the `https://developer.android.com/reference/androidx/graphics/shapes/Cubic`s' coordinates aren't valid representations of numbers. |

### serialize

```
public static final @NonNull String serialize(@NonNull List<@NonNull Feature> features)
```

Serializes a list of `https://developer.android.com/reference/androidx/graphics/shapes/Feature` objects into a string representation, adhering to version 1 of the feature serialization format.

**Format:**

1. **Version Identifier:** A 'V' followed by the version number (e.g., 'V1').

2. **Feature Serialization:** Each `https://developer.android.com/reference/androidx/graphics/shapes/Feature` is serialized individually and concatenated.

   - **Feature Tag:** A single-character tag identifies the feature type:

     - 'n': Edge

     - 'x': Convex Corner

     - 'o': Concave Corner

   - **Cubic Serialization:** Each `https://developer.android.com/reference/androidx/graphics/shapes/Cubic` within a feature is serialized as a sequence of comma-separated x,y coordinates. Subsequent cubics within a feature omit their first anchor points, as they're identical to their predecessors' last anchor points.

**Example:** Given two features:

- An edge with one `https://developer.android.com/reference/androidx/graphics/shapes/Cubic`: { (0, 0), (1, 1), (2, 2), (3, 3) }

- A convex corner with two `https://developer.android.com/reference/androidx/graphics/shapes/Cubic` objects: { (0, 0), (1, 1), (2, 2), (3, 3) }, { (3, 3), (4, 4), (5, 5), (6, 6) }

The serialized string would be:

```
V1 n 0,0, 1,1, 2,2, 3,3  x 0,0, 1,1, 2,2, 3,3, 4,4, 5,5, 6,6
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/graphics/shapes/Feature> features` | The list of `https://developer.android.com/reference/androidx/graphics/shapes/Feature` objects to serialize. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | The serialized string representation of the `https://developer.android.com/reference/androidx/graphics/shapes/FeatureSerializer.Companion#serialize(kotlin.collections.List)` |