ArcGIS API for Silverlight - Library Reference
FromJson Method
See Also  Example Send comments on this topic
ESRI.ArcGIS.Client.Geometry Namespace > Geometry Class : FromJson Method

json
The JSON to deserialize to Geometry.
Deserializes the JSON string to geometry.

Syntax

Visual Basic (Declaration) 
Public Shared Function FromJson( _
   ByVal json As String _
) As Geometry
C# 
public static Geometry FromJson( 
   string json
)

Remarks

The Geometry.FromJson() Method is a Shared/static function which means that you do not use the new keyword to instantiate it as an object.

The Geometry.FromJson() Method is a very fast operation as it processes exclusively on the Client; no round trip to ArcGIS Server is required.

If you do not pass a JSON string generated from a Geometry object to the Geometry.FromJson() Method, you will get back an Unhandled Exception unless you write error handling for it. If you trap for the any error messages for the Shared/static Geometry.FromJson() Method, its message will be similar to the following: "System.ArgumentException: Invalid JSON primitive".

Example of Geometry.FromJson error (System.ArcgumentException: Invalid JSON primitive).

NOTE: You should only pass JSON from Geometry objects into the Shared/static Geometry.FromJson() Method; do not use other JSON types (for example: JSON from an ESRI.ArcGIS.Client.Symbols.Symbol, etc.).

There are several use cases for using the Geometry.FromJson() and Geometry.ToJson() Methods. For example you could write custom undo/redo functions for Graphics editing. Another example would be to create a custom Server Object Extension (SOE) for an ArcObject's application.

Parameters

json
The JSON to deserialize to Geometry.

Return Value

The deserialized geometry.

Example

How to use:

Choose a Graphic type to draw from the ComboBox. Then draw a Graphic in left Map. In the code-behind the Geometry of the user draw Graphic will be converted to JSON and displayed in a TextBlock. A new Graphic will then be reconstituted from the JSON and drawn in the right Map.

The XAML code in this example is used in conjunction with the code-behind (C# or VB.NET) to demonstrate the functionality.

The following screen shot corresponds to the code example in this page.

Drawing Graphics on the Map using the Geometry.FromJson Method.

XAMLCopy Code
<Grid x:Name="LayoutRoot" Background="White">
            
  <!-- Add user defined resources for the symbology of the Graphics being drawn in the Map Controls. -->
  <Grid.Resources>
    <esri:SimpleLineSymbol x:Key="DrawLineSymbol" Color="Green" Width="4" />
    <esri:SimpleFillSymbol x:Key="DrawFillSymbol" Fill="#3300FF00" BorderBrush="Green" BorderThickness="2" />
    <esri:SimpleMarkerSymbol x:Key="DefaultMarkerSymbol" Color="Red" Size="12" Style="Circle" />
    <esri:SimpleLineSymbol x:Key="DefaultLineSymbol" Color="Red" Width="4" />
    <esri:SimpleFillSymbol x:Key="DefaultFillSymbol" Fill="#33FF0000" BorderBrush="Red" BorderThickness="2" />
    <esri:SimpleRenderer x:Key="PolygonRenderer" Symbol="{StaticResource DefaultFillSymbol}"/>
    <esri:SimpleRenderer x:Key="PolylineRenderer" Symbol="{StaticResource DefaultLineSymbol}"/>
    <esri:SimpleRenderer x:Key="PointRenderer" Symbol="{StaticResource DefaultMarkerSymbol}"/>
  </Grid.Resources>
  
  <!-- Add a Map Control. This will be the one the user draws the Graphics on. -->
  <TextBlock Height="23" HorizontalAlignment="Left" Margin="134,122,0,0" 
             Name="TextBlock_Label1" Text="Draw Graphics on this Map:" VerticalAlignment="Top" />
  <esri:Map Background="White" HorizontalAlignment="Left" Margin="134,144,0,0" Name="Map1" 
        VerticalAlignment="Top" WrapAround="True" Height="275" Width="225">
    <esri:ArcGISTiledMapServiceLayer Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/>
    <esri:GraphicsLayer ID="InputPolygonLayer" Renderer="{StaticResource PolygonRenderer}"/>
    <esri:GraphicsLayer ID="InputPolylineLayer" Renderer="{StaticResource PolylineRenderer}"/>
    <esri:GraphicsLayer ID="InputPointLayer" Renderer="{StaticResource PointRenderer}"/>
  </esri:Map>
  
  <!-- Add a Map Control. This will be the one that get Graphics drawn that are reconstituted from JSON. -->
  <TextBlock Height="36" HorizontalAlignment="left" Margin="375,109,0,0" Name="TextBlock_Label2" TextWrapping="Wrap" Width="200"
             Text="Graphics will be reconstituted from JSON and drawn on this Map:" VerticalAlignment="Top" />
  <esri:Map Background="White" HorizontalAlignment="Left" Margin="375,144,0,0" Name="Map2" 
        VerticalAlignment="Top" WrapAround="True" Height="275" Width="225">
    <esri:ArcGISTiledMapServiceLayer Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/>
    <esri:GraphicsLayer ID="JSON_PolygonLayer" Renderer="{StaticResource PolygonRenderer}"/>
    <esri:GraphicsLayer ID="JSON_PolylineLayer" Renderer="{StaticResource PolylineRenderer}"/>
    <esri:GraphicsLayer ID="JSON_PointLayer" Renderer="{StaticResource PointRenderer}"/>
  </esri:Map>
  
  <!-- ComboBox for the user specifying which type of Graphic to draw. -->
  <TextBlock Height="23" HorizontalAlignment="Left" Margin="0,122,0,0" Name="TextBlock_Label3" 
             Text="Pick a Graphic Type:" VerticalAlignment="Top" />
  <ComboBox x:Name="ComboBox_GraphicMode" Height="28" HorizontalAlignment="Left" Margin="0,144,0,0" VerticalAlignment="Top" 
            Width="120" SelectionChanged="Mode_SelectionChanged"/>
  
  <!-- Display the JSON string from the user drawn graphic. -->
  <sdk:Label Height="28" HorizontalAlignment="Left" Margin="0,447,0,0" 
       Name="Label_JSON" VerticalAlignment="Top" Width="236" Content="JSON from drawing of Graphic:"/>
  <TextBlock Height="133" HorizontalAlignment="Left" Margin="0,467,0,0" Name="TextBlock_JSON" Text="TextBlock" 
             VerticalAlignment="Top" Width="600" TextWrapping="Wrap"/>
  
  <!-- Provide the instructions on how to use the sample code. -->
  <TextBlock Height="102" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="600" TextWrapping="Wrap"
             Text="Choose a Graphic type to draw from the ComboBox. Then draw a Graphic in left Map. In the code-behind 
             the Geometry of the user draw Graphic will be converted to JSON and displayed in a TextBlock. 
             A new Graphic will then be reconstituted from the JSON and drawn in the right Map." />
  
</Grid>
C#Copy Code
// Member (i.e. Global) variable used for user drawn Graphics in the left Map (i.e. Map1). 
private ESRI.ArcGIS.Client.Draw _Draw;
            
public MainPage()
{
  InitializeComponent();
  
  // Populate the ComboBox with the various types of Graphics that can be drawn in the Map. 
  ComboBox_GraphicMode.ItemsSource = new ESRI.ArcGIS.Client.DrawMode[] 
  {
    ESRI.ArcGIS.Client.DrawMode.None, 
    ESRI.ArcGIS.Client.DrawMode.Point, 
    ESRI.ArcGIS.Client.DrawMode.Polyline, 
    ESRI.ArcGIS.Client.DrawMode.Polygon, 
    ESRI.ArcGIS.Client.DrawMode.Rectangle, 
    ESRI.ArcGIS.Client.DrawMode.Freehand, 
    ESRI.ArcGIS.Client.DrawMode.Arrow, 
    ESRI.ArcGIS.Client.DrawMode.Triangle, 
    ESRI.ArcGIS.Client.DrawMode.Ellipse, 
    ESRI.ArcGIS.Client.DrawMode.Circle, 
    ESRI.ArcGIS.Client.DrawMode.LineSegment 
  };
  
  // Instantiate the Draw object as new. Set the .Map, .LineSymbol, and .FillSymbol Properties to those specified
  // in XAML.
  _Draw = new ESRI.ArcGIS.Client.Draw();
  _Draw.Map = Map1;
  _Draw.LineSymbol = LayoutRoot.Resources["DrawLineSymbol"] as ESRI.ArcGIS.Client.Symbols.LineSymbol;
  _Draw.FillSymbol = LayoutRoot.Resources["DrawFillSymbol"] as ESRI.ArcGIS.Client.Symbols.FillSymbol;
  
  // Wire up an Event handler as a lambda expression to handle when the user completes drawing a Graphic
  // in the left Map (i.e. Map1).
  _Draw.DrawComplete += (a, b) =>
  {
    // Get the DrawEventArgs from the lambda expression.
    ESRI.ArcGIS.Client.DrawEventArgs theEventArgs = b;
    
    // Get the type of Geometry from the DrawEventArgs that the user draw on the left Map (i.e. Map1). 
    ESRI.ArcGIS.Client.Geometry.Geometry theUserDrawnGeometry = theEventArgs.Geometry;
    
    // Get the JSON string from the Geometry of the Graphic just drawn by the user.
    string jsonString = theUserDrawnGeometry.ToJson();
    
    // Display the JSON string back in a TextBlock.
    TextBlock_JSON.Text = jsonString;
    
    // Create a new Geometry object.
    ESRI.ArcGIS.Client.Geometry.Geometry theGeometryJSON = null;
    try
    {
      // NOTE: The ESRI.ArcGIS.Client.Geometry.Geometry.FromJson() is a static Method.
      theGeometryJSON = ESRI.ArcGIS.Client.Geometry.Geometry.FromJson(jsonString);
    }
    catch (Exception ex)
    {
      MessageBox.Show(ex.ToString());
    }
    
    // Branch based upon the type of Geometry being processed.
    if (theUserDrawnGeometry is ESRI.ArcGIS.Client.Geometry.MapPoint)
    {
      // Draw the Graphics in Map1 (user drawn Graphics).
      ESRI.ArcGIS.Client.GraphicsLayer theGraphicsLayer1 = null;
      theGraphicsLayer1 = Map1.Layers["InputPointLayer"] as ESRI.ArcGIS.Client.GraphicsLayer;
      theGraphicsLayer1.Graphics.Add(new ESRI.ArcGIS.Client.Graphic() { Geometry = theUserDrawnGeometry });
      
      // Draw the Graphics in Map2 (JSON reconstitued Graphics).
      ESRI.ArcGIS.Client.GraphicsLayer theGraphicsLayer2 = null;
      theGraphicsLayer2 = Map2.Layers["JSON_PointLayer"] as ESRI.ArcGIS.Client.GraphicsLayer;
      theGraphicsLayer2.Graphics.Add(new ESRI.ArcGIS.Client.Graphic() { Geometry = theGeometryJSON });
    }
    else if (theUserDrawnGeometry is ESRI.ArcGIS.Client.Geometry.Polyline)
    {
      // Draw the Graphics in Map1 (user drawn Graphics).
      ESRI.ArcGIS.Client.GraphicsLayer theGraphicsLayer1 = null;
      theGraphicsLayer1 = Map1.Layers["InputPolylineLayer"] as ESRI.ArcGIS.Client.GraphicsLayer;
      theGraphicsLayer1.Graphics.Add(new ESRI.ArcGIS.Client.Graphic() { Geometry = theUserDrawnGeometry });
      
      // Draw the Graphics in Map2 (JSON reconstitued Graphics).
      ESRI.ArcGIS.Client.GraphicsLayer theGraphicsLayer2 = null;
      theGraphicsLayer2 = Map2.Layers["JSON_PolylineLayer"] as ESRI.ArcGIS.Client.GraphicsLayer;
      theGraphicsLayer2.Graphics.Add(new ESRI.ArcGIS.Client.Graphic() { Geometry = theGeometryJSON });
    }
    else if (theUserDrawnGeometry is ESRI.ArcGIS.Client.Geometry.Polygon)
    {
      // Draw the Graphics in Map1 (user drawn Graphics).
      ESRI.ArcGIS.Client.GraphicsLayer theGraphicsLayer1 = null;
      theGraphicsLayer1 = Map1.Layers["InputPolygonLayer"] as ESRI.ArcGIS.Client.GraphicsLayer;
      theGraphicsLayer1.Graphics.Add(new ESRI.ArcGIS.Client.Graphic() { Geometry = theUserDrawnGeometry });
      
      // Draw the Graphics in Map2 (JSON reconstitued Graphics).
      ESRI.ArcGIS.Client.GraphicsLayer theGraphicsLayer2 = null;
      theGraphicsLayer2 = Map2.Layers["JSON_PolygonLayer"] as ESRI.ArcGIS.Client.GraphicsLayer;
      theGraphicsLayer2.Graphics.Add(new ESRI.ArcGIS.Client.Graphic() { Geometry = theGeometryJSON });
    }
    else if (theUserDrawnGeometry is ESRI.ArcGIS.Client.Geometry.Envelope)
    {
      // Draw the Graphics in Map1 (user drawn Graphics).
      ESRI.ArcGIS.Client.GraphicsLayer theGrasphicsLayer1 = null;
      theGrasphicsLayer1 = Map1.Layers["InputPolygonLayer"] as ESRI.ArcGIS.Client.GraphicsLayer;
      theGrasphicsLayer1.Graphics.Add(new ESRI.ArcGIS.Client.Graphic() { Geometry = theUserDrawnGeometry });
      
      // Draw the Graphics in Map2 (JSON reconstitued Graphics).
      ESRI.ArcGIS.Client.GraphicsLayer theGrasphicsLayer2 = null;
      theGrasphicsLayer2 = Map2.Layers["JSON_PolygonLayer"] as ESRI.ArcGIS.Client.GraphicsLayer;
      theGrasphicsLayer2.Graphics.Add(new ESRI.ArcGIS.Client.Graphic() { Geometry = theGeometryJSON });
    }
  };
  
  // Set the starting item in the ComboxBox to the first one.
  ComboBox_GraphicMode.SelectedIndex = 0;
}
  
private void Mode_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  // This function executes when the user changes the ComboBox choice to select a different type of
  // Graphic to draw.
  
  if (e.AddedItems == null || e.AddedItems.Count == 0)
  {
    // No change or invalid choice made.
    return;
  }
  
  // Enable the Draw object to accept user input for the particular Graphic type to be drawn.
  _Draw.DrawMode = (ESRI.ArcGIS.Client.DrawMode)(e.AddedItems[0]);
  _Draw.IsEnabled = _Draw.DrawMode != ESRI.ArcGIS.Client.DrawMode.None;
}
VB.NETCopy Code
' Member (i.e. Global) variable used for user drawn Graphics in the left Map (i.e. Map1). 
Private _Draw As ESRI.ArcGIS.Client.Draw
            
Public Sub New()
            
  InitializeComponent()
  
  ' Populate the ComboBox with the various types of Graphics that can be drawn in the Map. 
  ComboBox_GraphicMode.ItemsSource = New ESRI.ArcGIS.Client.DrawMode() {ESRI.ArcGIS.Client.DrawMode.None,
                                                        ESRI.ArcGIS.Client.DrawMode.Point,
                                                        ESRI.ArcGIS.Client.DrawMode.Polyline,
                                                        ESRI.ArcGIS.Client.DrawMode.Polygon,
                                                        ESRI.ArcGIS.Client.DrawMode.Rectangle,
                                                        ESRI.ArcGIS.Client.DrawMode.Freehand,
                                                        ESRI.ArcGIS.Client.DrawMode.Arrow,
                                                        ESRI.ArcGIS.Client.DrawMode.Triangle,
                                                        ESRI.ArcGIS.Client.DrawMode.Ellipse,
                                                        ESRI.ArcGIS.Client.DrawMode.Circle,
                                                        ESRI.ArcGIS.Client.DrawMode.LineSegment}
  
  ' Instantiate the Draw object as new. Set the .Map, .LineSymbol, and .FillSymbol Properties to those specified
  ' in XAML.
  _Draw = New ESRI.ArcGIS.Client.Draw()
  _Draw.Map = Map1
  _Draw.LineSymbol = TryCast(LayoutRoot.Resources("DrawLineSymbol"), ESRI.ArcGIS.Client.Symbols.LineSymbol)
  _Draw.FillSymbol = TryCast(LayoutRoot.Resources("DrawFillSymbol"), ESRI.ArcGIS.Client.Symbols.FillSymbol)
  
  ' Wire up an Event handler as a lambda expression to handle when the user completes drawing a Graphic
  ' in the left Map (i.e. Map1).
  AddHandler _Draw.DrawComplete,
   Sub(a, b)
   
    ' Get the DrawEventArgs from the lambda expression.
    Dim theEventArgs As ESRI.ArcGIS.Client.DrawEventArgs = b
    
    ' Get the type of Geometry from the DrawEventArgs that the user draw on the left Map (i.e. Map1). 
    Dim theUserDrawnGeometry As ESRI.ArcGIS.Client.Geometry.Geometry = theEventArgs.Geometry
    
    ' Get the JSON string from the Geometry of the Graphic just drawn by the user.
    Dim jsonString As String = theUserDrawnGeometry.ToJson()
    
    ' Display the JSON string back in a TextBlock.
    TextBlock_JSON.Text = jsonString
    
    ' Create a new Geometry object.
    Dim theGeometryJSON As ESRI.ArcGIS.Client.Geometry.Geometry = Nothing
    Try
      ' NOTE: The ESRI.ArcGIS.Client.Geometry.Geometry.FromJson() is a Shared Method.
      theGeometryJSON = ESRI.ArcGIS.Client.Geometry.Geometry.FromJson(jsonString)
    Catch ex As Exception
      MessageBox.Show(ex.ToString)
    End Try
    
    ' Branch based upon the type of Geometry being processed.
    If TypeOf theUserDrawnGeometry Is ESRI.ArcGIS.Client.Geometry.MapPoint Then
      
      ' Draw the Graphics in Map1 (user drawn Graphics).
      Dim theGraphicsLayer1 As ESRI.ArcGIS.Client.GraphicsLayer
      theGraphicsLayer1 = TryCast(Map1.Layers("InputPointLayer"), ESRI.ArcGIS.Client.GraphicsLayer)
      theGraphicsLayer1.Graphics.Add(New ESRI.ArcGIS.Client.Graphic() With {.Geometry = theUserDrawnGeometry})
      
      ' Draw the Graphics in Map2 (JSON reconstitued Graphics).
      Dim theGraphicsLayer2 As ESRI.ArcGIS.Client.GraphicsLayer
      theGraphicsLayer2 = TryCast(Map2.Layers("JSON_PointLayer"), ESRI.ArcGIS.Client.GraphicsLayer)
      theGraphicsLayer2.Graphics.Add(New ESRI.ArcGIS.Client.Graphic() With {.Geometry = theGeometryJSON})
      
    ElseIf TypeOf theUserDrawnGeometry Is ESRI.ArcGIS.Client.Geometry.Polyline Then
      
      ' Draw the Graphics in Map1 (user drawn Graphics).
      Dim theGraphicsLayer1 As ESRI.ArcGIS.Client.GraphicsLayer
      theGraphicsLayer1 = TryCast(Map1.Layers("InputPolylineLayer"), ESRI.ArcGIS.Client.GraphicsLayer)
      theGraphicsLayer1.Graphics.Add(New ESRI.ArcGIS.Client.Graphic() With {.Geometry = theUserDrawnGeometry})
      
      ' Draw the Graphics in Map2 (JSON reconstitued Graphics).
      Dim theGraphicsLayer2 As ESRI.ArcGIS.Client.GraphicsLayer
      theGraphicsLayer2 = TryCast(Map2.Layers("JSON_PolylineLayer"), ESRI.ArcGIS.Client.GraphicsLayer)
      theGraphicsLayer2.Graphics.Add(New ESRI.ArcGIS.Client.Graphic() With {.Geometry = theGeometryJSON})
      
    ElseIf TypeOf theUserDrawnGeometry Is ESRI.ArcGIS.Client.Geometry.Polygon Then
      
      ' Draw the Graphics in Map1 (user drawn Graphics).
      Dim theGraphicsLayer1 As ESRI.ArcGIS.Client.GraphicsLayer
      theGraphicsLayer1 = TryCast(Map1.Layers("InputPolygonLayer"), ESRI.ArcGIS.Client.GraphicsLayer)
      theGraphicsLayer1.Graphics.Add(New ESRI.ArcGIS.Client.Graphic() With {.Geometry = theUserDrawnGeometry})
      
      ' Draw the Graphics in Map2 (JSON reconstitued Graphics).
      Dim theGraphicsLayer2 As ESRI.ArcGIS.Client.GraphicsLayer
      theGraphicsLayer2 = TryCast(Map2.Layers("JSON_PolygonLayer"), ESRI.ArcGIS.Client.GraphicsLayer)
      theGraphicsLayer2.Graphics.Add(New ESRI.ArcGIS.Client.Graphic() With {.Geometry = theGeometryJSON})
      
    ElseIf TypeOf theUserDrawnGeometry Is ESRI.ArcGIS.Client.Geometry.Envelope Then
      
      ' Draw the Graphics in Map1 (user drawn Graphics).
      Dim theGrasphicsLayer1 As ESRI.ArcGIS.Client.GraphicsLayer
      theGrasphicsLayer1 = TryCast(Map1.Layers("InputPolygonLayer"), ESRI.ArcGIS.Client.GraphicsLayer)
      theGrasphicsLayer1.Graphics.Add(New ESRI.ArcGIS.Client.Graphic() With {.Geometry = theUserDrawnGeometry})
      
      ' Draw the Graphics in Map2 (JSON reconstitued Graphics).
      Dim theGrasphicsLayer2 As ESRI.ArcGIS.Client.GraphicsLayer
      theGrasphicsLayer2 = TryCast(Map2.Layers("JSON_PolygonLayer"), ESRI.ArcGIS.Client.GraphicsLayer)
      theGrasphicsLayer2.Graphics.Add(New ESRI.ArcGIS.Client.Graphic() With {.Geometry = theGeometryJSON})
      
    End If
    
  End Sub
    
  Set the starting item in the ComboxBox to the first one.
  ComboBox_GraphicMode.SelectedIndex = 0
   
End Sub
            
Private Sub Mode_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
            
  ' This function executes when the user changes the ComboBox choice to select a different type of
  ' Graphic to draw.
  
  If e.AddedItems Is Nothing OrElse e.AddedItems.Count = 0 Then
    ' No change or invalid choice made.
    Return
  End If
  
  ' Enable the Draw object to accept user input for the particular Graphic type to be drawn.
  _Draw.DrawMode = CType(e.AddedItems(0), ESRI.ArcGIS.Client.DrawMode)
  _Draw.IsEnabled = _Draw.DrawMode <> ESRI.ArcGIS.Client.DrawMode.None
  
End Sub

Requirements

Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7

See Also

© ESRI, Inc. All Rights Reserved.