Skip to content

JsonPathManager.Force(string path, object value) Method

Shuzhao Feng edited this page Jan 24, 2024 · 3 revisions

Note: as of v0.3.0, this method is obsolete. Use JsonPathManager.Add(string path, object value, Priority priority) with Priority.High instead to achieve the same functionality.

Definition

Force a value to the JsonPathManager tree.

Method Signature

JsonPathManager.Force(string path, object value);

Usage

JsonPathManager instance = new JsonPathManager();

instance.Force("$.path.to.location", "value");

Parameters

path : string

The path to the location where the value should be forced.

Supported Path Syntax

See JsonPathSerializer Supported Paths.

value : object

The value to force at the location specified by the path.

Difference Between JsonPathManager.Add and JsonPathManager.Force

Both methods would add an object at the given path. However, JsonPathManager.Add throws exceptions at some cases where the operations might unintentionally override some existing values on the root, whereas JsonPathManager.Force is guaranteed to succeed.

For example:

  • Given root { "name" : "John" }, JsonPathManager.Add("$.name.first", "John") throws an exception, but JsonPathManager.Force("$.name.first", "John") would yield { "name" : { "first" : "John" } };

  • Given root { "name" : { "first" : "John" } }, JsonPathManager.Add("$.name", "John") throws an exception, but JsonPathManager.Force("$.name", "John") would yield { "name" : "John" };

  • Given root { "name" : { "first" : "John" } }, JsonPathManager.Add("$.name[0]", "John") throws an exception, but JsonPathManager.Force("$.name[0]", "John") would yield { "name" : [ "John" ] };

Supported Versions

v0.1.2 and above
  • v0.3.0
  • v0.2.0
  • v0.1.2

Clone this wiki locally