def METHOD_NAME(resource, incoming_metadata):
    """
    Writes resource metadata json to the resource rdf/xml.
    Parameters:
    :param resource: A resource django model instance
    :param incoming_metadata: JSON dictionary of resource metadata
    :returns: ResourceMetadata schema instance
    :raises: ValidationError when incoming_metadata does not pass validation
    """
    r_md = resource_metadata(resource).dict()
    try:
        incoming_r_md = ResourceMetadataInForbidExtra(**incoming_metadata)
    except PydanticValidationError as e:
        raise ValidationError(e)
    # merge existing metadata with incoming, incoming overrides existing
    merged_metadata = {
        **r_md,
        **incoming_r_md.dict(exclude_defaults=True),
        "modified": datetime.now(timezone.utc).isoformat(),
    }
    res_metadata = ResourceMetadata(**merged_metadata)
    graph = rdf_graph(res_metadata)
    try:
        with transaction.atomic():
            resource.metadata.delete_all_elements()
            resource.metadata.ingest_metadata(graph)
    except: # noqa
        logger.exception(
            f"Error processing resource metadata file for resource {resource.short_id}"
        )
        raise
    save_resource_metadata_xml(resource)
    return json.loads(res_metadata.json())