import sys
import json

def ftag(t):
    return lambda node: node.tag == t

def ustr(s):
    if sys.version_info < (3, 0):
        #py2
        return unicode(s).encode('utf-8')
    else:
        return str(s)

class TableJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if hasattr(obj, '__json__'):
            return obj.__json__()
        return json.JSONEncoder.default(self, obj)

import sys
import json

def ftag(t):
    return lambda node: node.tag == t

def ustr(s):
    if sys.version_info < (3, 0):
        #py2
        try:
            return unicode(s).encode('utf-8')
        except UnicodeDecodeError:
            return str(s)
    else:
        return str(s)

class TableJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if hasattr(obj, '__json__'):
            return obj.__json__()
        return json.JSONEncoder.default(self, obj)

Add try catch to avoid unicode decode exception in python 2.7.10
Add try catch to avoid unicode decode exception in python 2.7.10

  import sys
  import json
  
  def ftag(t):
      return lambda node: node.tag == t
  
  def ustr(s):
      if sys.version_info < (3, 0):
          #py2
+         try:
-         return unicode(s).encode('utf-8')
+             return unicode(s).encode('utf-8')
+         except UnicodeDecodeError:
+             return str(s)
      else:
          return str(s)
  
  class TableJSONEncoder(json.JSONEncoder):
      def default(self, obj):
          if hasattr(obj, '__json__'):
              return obj.__json__()
          return json.JSONEncoder.default(self, obj)
  
Add try catch to avoid unicode decode exception in python 2.7.10
## Code Before:
import sys
import json

def ftag(t):
    return lambda node: node.tag == t

def ustr(s):
    if sys.version_info < (3, 0):
        #py2
        return unicode(s).encode('utf-8')
    else:
        return str(s)

class TableJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if hasattr(obj, '__json__'):
            return obj.__json__()
        return json.JSONEncoder.default(self, obj)

## Instruction:
Add try catch to avoid unicode decode exception in python 2.7.10
## Code After:
import sys
import json

def ftag(t):
    return lambda node: node.tag == t

def ustr(s):
    if sys.version_info < (3, 0):
        #py2
        try:
            return unicode(s).encode('utf-8')
        except UnicodeDecodeError:
            return str(s)
    else:
        return str(s)

class TableJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if hasattr(obj, '__json__'):
            return obj.__json__()
        return json.JSONEncoder.default(self, obj)
